我想知道如何为Android 4.0的SearchView
设计下拉弹出窗口样式?
我正在使用Theme.Sherlock.Light.DarkActionBar
,但我不知道如何将下拉搜索的背景变成白色,文字变成黑色?
我想知道如何为Android 4.0的SearchView
设计下拉弹出窗口样式?
我正在使用Theme.Sherlock.Light.DarkActionBar
,但我不知道如何将下拉搜索的背景变成白色,文字变成黑色?
由于某些原因,使用“searchAutoCompleteTextView”主题也对我不起作用。 因此,在设置SearchView时,我使用以下代码解决了它:
注意:这全部是通过android v7支持/AppCompat库完成的。
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Theme the SearchView's AutoCompleteTextView drop down. For some reason this wasn't working in styles.xml
SearchAutoComplete autoCompleteTextView = (SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
if (autoCompleteTextView != null) {
autoCompleteTextView.setDropDownBackgroundResource(R.drawable.abc_search_dropdown_light);
}
}
兼容性库提供了两种搜索下拉资源,它们分别是
有多个步骤需要完成
首先,您需要创建一个自定义的drawable,具有四种状态,可以参考{ABSLibrary}/res/drawable/abs__list_selector_holo_dark.xml。它将是这样的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_disabled_holo_dark" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/abs__list_selector_disabled_holo_dark" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_dark" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_dark" />
<item android:state_focused="true" android:drawable="@drawable/abs__list_focused_holo" />
将上述自定义可绘制对象(.xml格式)保存到项目res/drawable目录中。通过参考上面的示例,相应地编辑样式。请注意,样式可能会深度嵌套,只需耐心查看树形结构即可。
然后创建(或将其放入现有的自定义主题中)具有以下内容的自定义主题,它应该保存为res/values/styles.xml:
<style name="Theme.MyCustomTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="searchAutoCompleteTextView">@style/MySearchAutoCompleteTextView</item></style>
<style name="MySearchAutoCompleteTextView" parent="Sherlock.__Widget.SearchAutoCompleteTextView">
<item name="android:dropDownSelector">@drawable/myCustomDrawable_DropDown</item>
<item name="android:popupBackground">@drawable/myCustomDrawable_popupBackground</item></style>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyCustomTheme" > ...