14

在我的应用程序中,我有一个显示搜索建议的搜索视图。我希望下拉/微调和文字是一种特定的颜色。目前,我已经能够做出唯一的变化是通过以下输入的文本的文本颜色:样式安卓搜索视图和下拉列表中的操作栏

<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+

回答

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

jedikv在这里尝试一个清洁剂soution:'SearchView.SearchAutoComplete autoComplete =(SearchView.SearchAutoComplete)searchView.findViewById(R.id.search_src_text); autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);' – darkangelo

+0

这是唯一对我有用的解决方案。谢谢 – Nico

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> 

14

在你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> 

这可以改变下拉项目的文字颜色。试试。

+0

这适用于我。 –

+0

没有AppCompat怎么样? – Yar

+0

@Yar将'myDropDownItemStyle'的父类型更改为非AppCompat样式。你的基本主题是什么? – huangming

3

使用样式你,如果你正在使用android.support.v7.widget改变下拉菜单的背景和项目文本颜色

<!-- 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> 
8

您可以在res/styles.xml定义以下样式.SearchView。

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

如果您使用的是默认搜索查看,属性应该是 “android:autoCompleteTextViewStyle” 和 “机器人:textAppearanceSearchResultTitle”。我写了一个post描述这个细节。

+0

你的帖子帮了我很多,非常感谢! – ukama

+0

是Android 4.2上唯一的解决方案。谢谢! – Pablo

+0

为我和博客文章工作很好,简洁,谢谢! – henry000