2014-12-02 148 views
7

我有一個內容提供程序的searchview,用於顯示在下拉菜單中的自定義建議。然而,下拉菜單背景是黑色的,而文字是黑色的,所以它不是很明顯。我的應用程序主題是從Theme.AppCompat.Light繼承的,所以我的應用程序中的其他內容都是淺色背景上的黑色文本。更改searchview自動完成下拉菜單的背景顏色

enter image description here

我想改變下拉的背景,使文本是可讀的,但我還沒有發現這樣做的任何方式。我得到的最接近的解決方案如下:Style Android SearchView Drop down popup 但是當下拉菜單出現時,東西看起來很亂。

enter image description here

對此有任何可行的解決方案?

+0

你嘗試克里斯詛咒的建議嗎? http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html。查找部分標題SearchView Widget。 – 2014-12-03 19:05:59

回答

9

這是一種可能性。

定義查詢建議行佈局,例如, R.layout.li_query_suggestion,看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    android:id="@android:id/text1" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:gravity="center_vertical" 
    android:minHeight="56dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:textColor="@android:color/black" 
    tools:text="Blah blah blah"/> 

注意,背景色爲白色,文本顏色爲黑色。當然,你可以改變這些值。

然後,在您的遊標適配器中,指定您作爲行佈局創建的佈局。例如:

CursorAdapter suggestionAdapter = new SimpleCursorAdapter(
    getActivity(), 
    R.layout.li_query_suggestion, 
    null, 
    new String[]{"name"}, 
    new int[]{android.R.id.text1}, 
    0); 

這會給你的東西看起來像下面這樣: enter image description here

僅供參考,我目前的主題是這樣的:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="colorPrimary">@color/light_blue_500</item> 
    <item name="colorPrimaryDark">@color/light_blue_700</item> 
    <item name="colorAccent">@color/pink_a200</item> 
    <item name="android:windowActionBarOverlay">true</item> 
    <item name="searchViewStyle">@style/CustomSearchView</item> 
</style> 
6

我認爲你應該使用

SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); 
if (autoCompleteTextView != null) { 
    autoCompleteTextView.setDropDownBackgroundDrawable(getResources().getDrawable(R.drawable.abc_popup_background_mtrl_mult)); 
} 

這可以設置SearchAutoComplete下拉框背景d光

14

應用樣式您可以更改下拉菜單背景顏色和文字的物品顏色和大小

<!-- 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> 
相關問題