【问题标题】:TextColor of Xamarin.Forms SearchBarXamarin.Forms SearchBar 的 TextColor
【发布时间】:2014-10-26 21:27:44
【问题描述】:

我在 StackLayout 上有一个 Xamarin.Forms SearchBar,背景颜色为深色。

默认情况下,输入字段也是黑暗的,可能是由于透明背景。要修改搜索栏背景颜色,有一个 BackgroundColor 属性。 (在下面的示例中,我将其设置为深灰色。)

但是如何调整文字颜色呢?没有这样的财产。即使使用自定义搜索栏渲染器,我也找不到解决方案。

这是仅使用占位符的外观:

...并带有一些输入(黑色“Hello world!”在相当黑暗的背景上):

【问题讨论】:

    标签: android xamarin xamarin.forms searchbar custom-renderer


    【解决方案1】:

    这里是一个可以解决问题的 Android 自定义渲染器的代码。我会上传截图,但我还没有足够的积分:(

    using System;
    
    using Android.Widget;
    using Android.Text;
    using G = Android.Graphics;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly:ExportRenderer( typeof(MySearchBar), typeof(Some.Namespance.MySearchBar_Droid) )]
    
    namespace Some.Namespace
    {
        public class MySearchBar_Droid : SearchBarRenderer
        {
            protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
            {
                base.OnElementChanged( args );
    
                // Get native control (background set in shared code, but can use SetBackgroundColor here)
                SearchView searchView = (base.Control as SearchView);
                searchView.SetInputType( InputTypes.ClassText | InputTypes.TextVariationNormal );
    
                // Access search textview within control
                int textViewId = searchView.Context.Resources.GetIdentifier( "android:id/search_src_text", null, null );
                EditText textView = (searchView.FindViewById( textViewId ) as EditText);
    
                // Set custom colors
                textView.SetBackgroundColor( G.Color.Rgb( 225, 225, 225 ) );
                textView.SetTextColor( G.Color.Rgb( 32, 32, 32 ) );
                textView.SetHintTextColor( G.Color.Rgb( 128, 128, 128 ) );
    
                // Customize frame color
                int frameId = searchView.Context.Resources.GetIdentifier( "android:id/search_plate", null, null );
                Android.Views.View frameView = (searchView.FindViewById( frameId ) as Android.Views.View);
                frameView.SetBackgroundColor( G.Color.Rgb( 96, 96, 96 ) );
            }
        }
    }
    

    【讨论】:

    • 我猜现在你可以上传了:)
    【解决方案2】:

    您可以通过创建具有 可绑定属性 的自定义 View(如 ForegroundTextColor)然后创建自定义渲染器来实现此目的。 p>

    在自定义渲染器中,您可以从平台特定的渲染器继承,即在 WindowsPhone 上它是:-

    Xamarin.Forms.Platform.WinPhone.SearchBarRenderer

    然后您可以监视您创建的 bindable-property 的属性更改并设置使用的 平台原生控件 Text-color更改 SearchBar 的非编辑视图的外观。

    您还必须至少监视 IsFocused 并在 WindowsPhone 上应用颜色。这也可能适用于其他平台。

    更新 1:-

    ========

    在回复您的评论时,您不必自己呈现整个 SearchBar

    如果您从 renderer 继承,您可以更好地自定义内容。

    具体参考 Android 来实现这一点,你必须参考 AutoCompleteTextView 然后你可以调用 SetTextColor 来改变颜色。

    更新 2:-

    ==========

    SearchBarAndroid 中的复合控件。

    AutoCompleteTextView 在层次结构中埋藏得相当深。

    4.4 上,可以使用以下代码找到。其他版本可能会有所不同。然而,这绝不是使用序数索引进行生产的好方法:-

    AutoCompleteTextView objAutoTextView = (AutoCompleteTextView)(((this.Control.GetChildAt(0) as ViewGroup).GetChildAt(2) as ViewGroup).GetChildAt(1) as ViewGroup).GetChildAt(0);
    

    thisrenderer 类。

    然后可以用颜色调用objAutoTextView.SetTextColor,实现SearchBar前景文本的颜色变化。

    【讨论】:

    • 不幸的是,这个解决方案不允许我设置 editable 文本的颜色。 SearchBar 似乎没有这样的属性(至少在 Android 上)。而且我不想自己渲染整个搜索栏。
    • 我已经用 Android 的特定更新信息更新了我的答案,请参阅(更新 1)。该方法无需重写 SearchBar 即可工作。
    • 我在SearchBarSearchBarRenderer 中找不到AutoCompleteTextView。 ://
    • Android 上,它被埋在层次结构的深处。我已经用 Android 的更多和代码更新了我的答案,请参阅(更新 2)
    • 太好了,更新 2 正是我想要的。它适用于我的应用程序。谢谢!
    【解决方案3】:

    我终于找到了一个非常简单的解决方案:

    我不小心使用了 Android Theme @android:style/Theme.Holo.Light 并明确修改了所有颜色。但是要在搜索栏上获得明亮的文本颜色,您最好使用@android:style/Theme.Holo

    它不允许您非常精确地设置颜色,但在这种情况下,我只需要将它从黑色更改为白色。

    【讨论】:

      【解决方案4】:

      如果您使用xaml获取不同平台搜索栏的不同文本颜色,可以在xaml中进行如下修改:

      <SearchBar x:Name="Search"
                 Placeholder="Search"    
                 TextChanged="SearchBar_OnTextChanged"  
                 SearchButtonPressed="OnSearch" BackgroundColor="#19588F">
               <SearchBar.TextColor>
                   <OnPlatform x:TypeArguments="Color">
                          <OnPlatform.iOS>
                            Black
                          </OnPlatform.iOS>
                          <OnPlatform.Android>
                            White
                          </OnPlatform.Android>
                          <OnPlatform.WinPhone>
                            White
                          </OnPlatform.WinPhone>
                   </OnPlatform>
               </SearchBar.TextColor>
       </SearchBar> 
      

      【讨论】:

      • 这帮助我改变了字体颜色。
      【解决方案5】:

      我是这样做的,另一种方式:

      [assembly: ExportRenderer(typeof (CustomSearchBar), typeof (CustomSearchBarRenderer))]
      
      namespace Bahai.Android.Renderers 
      {
      public class CustomSearchBarRenderer : SearchBarRenderer
      {
          protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
          {
              base.OnElementChanged(e);
              if (e.OldElement == null)
              {
                  SearchBar element = (SearchBar) this.Element;
                  var native = (global::Android.Widget.SearchView) Control;
      
                  // do whatever you want to the controls here!
                  //--------------------------------------------
                  // element.BackgroundColor = Color.Transparent;
                  // native.SetBackgroundColor(element.BackgroundColor.ToAndroid());
                  // native.SetBackgroundColor(Color.White.ToAndroid());
      
                  //The text color of the SearchBar / SearchView 
                  AutoCompleteTextView textField = (AutoCompleteTextView)
                      (((Control.GetChildAt(0) as ViewGroup)
                          .GetChildAt(2) as ViewGroup)
                          .GetChildAt(1) as ViewGroup)
                          .GetChildAt(0);
      
                  if (textField != null)
                      textField.SetTextColor(Color.White.ToAndroid());
              }
          }
         }
      }
      

      【讨论】:

        【解决方案6】:

        SearchBar 有一个 TextColor 属性。至少在 Xamarin 5 中。

        <SearchBar
                TextColor="Black"
                Text="{Binding SearchString}">
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-20
          • 1970-01-01
          • 2019-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          相关资源
          最近更新 更多