我在 StackLayout 上有一个 Xamarin.Forms SearchBar,背景颜色为深色。
默认情况下,输入字段也是黑暗的,可能是由于透明背景。要修改搜索栏背景颜色,有一个BackgroundColor
属性。(在下面的示例中,我将其设置为深灰色。)
但是如何调整文字颜色呢?没有这样的财产。即使使用自定义搜索栏渲染器,我也找不到解决方案。
这是仅使用占位符的外观:
...并有一些输入(黑色“Hello world!”在相当黑暗的背景上):
我在 StackLayout 上有一个 Xamarin.Forms SearchBar,背景颜色为深色。
默认情况下,输入字段也是黑暗的,可能是由于透明背景。要修改搜索栏背景颜色,有一个BackgroundColor
属性。(在下面的示例中,我将其设置为深灰色。)
但是如何调整文字颜色呢?没有这样的财产。即使使用自定义搜索栏渲染器,我也找不到解决方案。
这是仅使用占位符的外观:
...并有一些输入(黑色“Hello world!”在相当黑暗的背景上):
这是一个可以解决问题的 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 ) );
}
}
}
您可以通过创建具有可绑定属性(如ForegroundTextColor )的自定义View来实现此目的,然后创建自定义渲染器。
在自定义渲染器中,您可以从平台特定的渲染器继承,即在WindowsPhone上它是:-
Xamarin.Forms.Platform.WinPhone.SearchBarRenderer
然后,您可以监视您创建的bindable-property的属性更改,并设置平台本机控件 Text-color用于更改SearchBar的非编辑视图的外观。
您还必须至少监视IsFocused并在WindowsPhone上应用着色。这也可能适用于其他平台。
更新1:-
========
在回复您的评论时,您不必自己呈现整个SearchBar。
如果您从渲染器继承,您可以更好地自定义事物。
具体参考Android来实现这一点,您必须参考AutoCompleteTextView,然后您可以调用SetTextColor来更改颜色。
更新 2:-
==========
SearchBar是Android中的复合控件。
AutoCompleteTextView在层次结构中埋藏得相当深。
在4.4上,可以使用以下代码找到。其他版本可能会有所不同。但是,这绝不是使用序数索引进行生产的好方法:-
AutoCompleteTextView objAutoTextView = (AutoCompleteTextView)(((this.Control.GetChildAt(0) as ViewGroup).GetChildAt(2) as ViewGroup).GetChildAt(1) as ViewGroup).GetChildAt(0);
这是渲染器类。
然后您可以使用颜色调用objAutoTextView.SetTextColor以实现对SearchBar前景文本的颜色更改。
我终于找到了一个非常简单的解决方案:
我不小心使用了 Android 主题@android:style/Theme.Holo.Light
并明确修改了所有颜色。但是要在搜索栏上获得明亮的文本颜色,您最好使用@android:style/Theme.Holo
.
它不允许您非常精确地设置颜色,但是在这种情况下,我只需要将其从黑色更改为白色。
如果您使用 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>
这是我如何做到的,另一种方式:
[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());
}
}
}
}
SearchBar 有一个 TextColor 属性。至少在 Xamarin 5 中。
<SearchBar
TextColor="Black"
Text="{Binding SearchString}">