更改searchview自动完成下拉菜单的背景颜色
问题描述:
我有一个内容提供程序的searchview,用于显示在下拉菜单中的自定义建议。然而,下拉菜单背景是黑色的,而文字是黑色的,所以它不是很明显。我的应用程序主题是从Theme.AppCompat.Light继承的,所以我的应用程序中的其他内容都是浅色背景上的黑色文本。更改searchview自动完成下拉菜单的背景颜色
我想改变下拉的背景,使文本是可读的,但我还没有发现这样做的任何方式。我得到的最接近的解决方案如下:Style Android SearchView Drop down popup 但是当下拉菜单出现时,东西看起来很乱。
对此有任何可行的解决方案?
答
这是一种可能性。
定义查询建议行布局,例如, 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);
这会给你的东西看起来像下面这样:
仅供参考,我目前的主题是这样的:
<!-- 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>
答
我认为你应该使用
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光
答
应用样式您可以更改下拉菜单背景颜色和文字的物品颜色和大小
<!-- 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>
你尝试克里斯诅咒的建议吗? http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html。查找部分标题SearchView Widget。 – 2014-12-03 19:05:59