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

时间:2015-01-05 23:16:53

标签: android android-actionbar android-styles searchview 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>

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

Here is how it currently looks

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

我的目标是API 19 +

6 个答案:

答案 0 :(得分: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>

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

答案 1 :(得分:9)

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

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);

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

答案 2 :(得分: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>

如果你使用默认的SearchView,那么属性应该是&#34; android:autoCompleteTextViewStyle&#34;和&#34; android:textAppearanceSearchResultTitle&#34;。我写了post来描述有关此内容的详细信息。

答案 3 :(得分:3)

使用样式可以更改下拉列表的背景和项目文本颜色

<!-- ToolBar -->
<style name="ToolBarStyle" parent="Theme.AppCompat">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="actionMenuTextColor">@android:color/white</item>
    <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item>
    <item name="android:dropDownListViewStyle">@style/myDropDownListViewStyle</item>
</style>


<style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner">
    <item name="android:textColor">@color/secondary_text_default_material_light</item>
    <item name="android:textSize">14dp</item>
</style>

<style name="myDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown">
    <item name="android:background">#FFF</item>
</style>

答案 4 :(得分:1)

您可以在themes.xml中定义suggestionRowLayout:

<resources>
<style name="AppTheme" parent="AppTheme.Base">
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/bluegrey_500</item>
    <item name="colorPrimaryDark">@color/bluegrey_900</item>
    <item name="colorAccent">@color/teal_500</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
    <!-- The layout for the search view. Be careful. -->
    <!--<item name="layout">...</item>-->
    <!-- Background for the search query section (e.g. EditText) -->
    <!--<item name="queryBackground">@color/bluegrey_400</item>-->
    <!-- Background for the actions section (e.g. voice, submit) -->
    <!--<item name="submitBackground">...</item>-->
    <!-- Close button icon -->
    <!--<item name="closeIcon">...</item>-->
    <!-- Search button icon -->
    <!--<item name="searchIcon">...</item>-->
    <!-- Go/commit button icon -->
    <!--<item name="goIcon">...</item>-->
     <!--Voice search button icon-->
    <!--<item name="voiceIcon">@drawable/abc_ic_voice_search_api_mtrl_alpha</item>-->
    <!-- Commit icon shown in the query suggestion row -->
    <!--<item name="commitIcon">...</item>-->
    <!-- Layout for query suggestion rows -->
    <item name="suggestionRowLayout">@layout/item_suggestion_place</item>
</style>

答案 5 :(得分:1)

我遇到了同样的问题(建议在白色背景上显示白色文字),但是所有答案对我来说都不起作用。因此,我最后通过将附加到 AutoCompleteTextView adapter TextView textColor 属性进行了解决:

代码:

AutoCompleteTextView autoComplete = ...;
List<String> suggestions = Arrays.asList("a", "b", "c");
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.dropdown_item, suggestions);
autoComplete.setAdapter(adapter);

dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:textColor="#000000"
      ...
/>