Android如何在操作栏中为SearchView设置样式?

6
我需要更改位于我的 ActionBar 中的 SearchView 样式。特别是我需要更改文本提示颜色以及如果可能的话,SearchView 图标。我可以在 XML 中实现这个吗?(在 XML 中是否可能实现?)
如果这不可能,我怎样才能很容易地通过自定义视图重新创建类似的行为? SearchView 的关闭/展开行为对我特别有用。

你找到这个问题的解决方案了吗?请不要忘记在这里通知我们 - 或者如果没有指定你的问题,请说明。 :-) - Tobias Reich
我成功地修改了提示颜色,方法如下:https://dev59.com/h2Mm5IYBdhLWcg3wMs3R - argenkiwi
4个回答

7
您是否尝试过这样的方法(使用ActionBarSherlock):
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(R.color.white));
searchText.setTextColor(getResources().getColor(R.color.white));

对我来说,它很好用。


你正在使用ActionBarSherlock。 - wangqi060934
1
这对我来说很有效,使用了ActionBarSherlock和android:minSdkVersion="8"。谢谢! - Munchies
使用AppCompat v21(Material,Lollipop)时,请使用以下代码: final AutoCompleteTextView searchText =(AutoCompleteTextView)searchView.findViewById(R.id.search_src_text); - araks

1

0
你需要创建一个新的主题,并在清单文件中使用它。
<style name="Theme.Test" parent="@style/Theme.AppCompat.Light">
   <item name="searchViewAutoCompleteTextView">@style/Theme.Test.SearchField</item>
</style>

<!-- Search edit text -->
<style name="Theme.Test.SearchField" parent="@style/Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/white</item>
</style>

对我来说它运行得很好。


0

正确的做法是使用自定义样式重载操作栏小部件样式。对于带有深色操作栏的 Holo Light,将以下内容放入您自己的样式文件中,例如res/values/styles_mytheme.xml

<style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarWidgetTheme">@style/Theme.MyTheme.Widget</item>
    <!-- your other custom styles -->
</style>

<style name="Theme.MyTheme.Widget" parent="@android:style/Theme.Holo">
    <item name="android:textColorHint">@android:color/white</item>
    <!-- your other custom widget styles -->
</style>

请确保您的应用程序使用自定义主题作为描述链接在此输入


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接