Android搜索框下拉弹出菜单样式

11

我想知道如何为Android 4.0的SearchView设计下拉弹出窗口样式?

我正在使用Theme.Sherlock.Light.DarkActionBar,但我不知道如何将下拉搜索的背景变成白色,文字变成黑色?

2个回答

8

由于某些原因,使用“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);
    }
}

兼容性库提供了两种搜索下拉资源,它们分别是

  • R.drawable.abc_search_dropdown_light(浅色背景)
  • R.drawable.abc_search_dropdown_dark(深色背景)

1
那个很好用,除了文本颜色不对,但我会再看看。 - Benoit
@Benoit 这是一个更改文本颜色的示例 - https://dev59.com/gmgu5IYBdhLWcg3wbWo9#14364222 - jimmithy
R.id.search_src_text 是什么? - Nishant Shah
@NishantShah 这是 AutoCompleteTextView 的资源 ID。 - jimmithy

2

有多个步骤需要完成

首先,您需要创建一个自定义的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>

请注意,“myCustomDrawable_DropDown”和“myCustomDrawable_popupBackground”必须是您刚创建的自定义drawable的名称。
您只需要知道,“android:dropDownSelector”和/或“android:popupBackground”负责自动完成弹出框的主题。
最后,在您的清单中应用主题!
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.MyCustomTheme" > ...

谢谢您的回答,我已经有一个自定义主题了,但是我缺少“searchAutoCompleteTextView”,即使有这个项目,我似乎也无法更改背景颜色。也许有些东西正在覆盖它。我需要深入挖掘一下。 - Benoit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接