【问题标题】:Android SearchView Text colorAndroid SearchView 文字颜色
【发布时间】:2014-12-29 02:53:02
【问题描述】:
我正在尝试将用户键入的搜索视图文本设为白色。 “android:searchViewTextField”给出错误。我找不到全局样式的正确名称:
<style name="AppTheme" parent="@style/_AppTheme"/>
<style name="_AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:searchViewTextField">@color/white_color</item>
</style>
是否有一个全局样式会只影响 Searchview 的文本而不是所有的文本框?
【问题讨论】:
标签:
android
xamarin
styles
searchview
【解决方案1】:
定义主题“SearchTextViewTheme”
<style name="SearchTextViewTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textColor">@color/white_color</item>
</style>
然后在TextView里面
<TextView
style="@style/SearchTextViewTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
实际上应用主题必须在布局 XML 中明确定义。因此您不必担心主题会影响其他文本框
【解决方案2】:
如果您的目标 SDK 为 20 或更少,则 Goolge 用于设置 SearchView 样式的属性将被隐藏,如果您尝试在主题中覆盖它们,最终会出现编译错误。
您可以使用 AppCompat v20 SearchView 而不是在此 tutorial 之后的本机 SearchView。 AppCompat 公开了searchViewTextField 和searchViewAutoCompleteTextView 等属性来更改AutoCompleteTextView 的样式。
【解决方案3】:
这是在Xamarin 中的做法(我的想法来自 Patrick 的 回答):
[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());
}
}
}
}
【解决方案4】:
这成功了
<style name="myTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:editTextColor">#fffff</item>
</style>
上述解决方案可能适用于 java,但它们不适用于 xamarin,我想这个解决方案将适用于 java。