在操作栏中设置android搜索视图和下拉列表的样式

Kur*_*hil 14 android android-actionbar searchview android-styles android-toolbar

在我的应用中,我有一个显示搜索建议的搜索视图.我希望下拉/微调器和文本是一种特定的颜色.目前我唯一能做的改变是输入文本的文字颜色如下:

   <style name="MyApp.AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:textColor">@color/white</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:background">@color/myBackgroundColor</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

该应用当前显示的搜索结果结果如下:

这是它目前的样子

我想知道的是如何更改searchHint颜色,下拉背景和下拉项目文本颜色等部分?

我的目标是API 19+

小智 15

在styles.xml中,将以下内容设置为AppTheme

<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
    <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item>
</style>

<style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner">
    <item name="android:textColor">@color/secondary_text_default_material_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这可以更改下拉项目文本颜色.试试.


Kur*_*hil 9

经过大量的搜索,我能找到的最接近的解决方案如下(感谢GZ95的回答!):

SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
//Set the input text color
autoComplete.setTextColor(Color.WHITE);
// set the hint text color
autoComplete.setHintTextColor(Color.WHITE);
//Some drawable (e.g. from xml)
autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);
Run Code Online (Sandbox Code Playgroud)

它不是最优雅的,但它对我有用.我唯一的问题是如何更改建议的项目文本颜色.我很高兴听到这个问题的更优化解决方案

  • jedikv在这里尝试更清洁的方法:`SearchView.SearchAutoComplete autoComplete =(SearchView.SearchAutoComplete)searchView.findViewById(R.id.search_src_text); autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);` (2认同)

t-b*_*nze 8

如果您使用的是android.support.v7.widget.SearchView,则可以在res/styles.xml中定义以下样式.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="autoCompleteTextViewStyle">@style/myAutoCompleteTextViewStyle</item>
    <item name="textAppearanceSearchResultTitle">@style/mySearchResult</item>
</style>

<style name="myAutoCompleteTextViewStyle" parent="Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:popupBackground">#ffffff</item>
</style>

<style name="mySearchResult" parent="TextAppearance.AppCompat.SearchResult.Title">
    <item name="android:textColor">#000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是默认的SearchView,则属性应为"android:autoCompleteTextViewStyle"和"android:textAppearanceSearchResultTitle".我写了一篇文章描述了这方面的细节.