【问题标题】:Style Android SearchView Drop down popup样式 Android SearchView 下拉弹出窗口
【发布时间】:2012-10-05 15:22:08
【问题描述】:

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

我正在使用Theme.Sherlock.Light.DarkActionBar,但我不知道如何将下拉搜索设置为白色背景和黑色文本?

【问题讨论】:

    标签: android actionbarsherlock android-ui android-theme


    【解决方案1】:

    由于某种原因,使用“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(深色背景)

    【讨论】:

    • 效果很好,只是文字颜色不对,但我会看更多
    • @Benoit Here' 一个如何更改文本颜色的示例 - stackoverflow.com/a/14364222/1299623
    • 什么是 R.id.search_src_text ?
    • @NishantShah 是 AutoCompleteTextView 的资源 id。
    【解决方案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" />
    

    将上面的自定义drawable(.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”负责自动完成弹出框的主题化。

    最后,在您的 Manifest 中应用主题!

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.MyCustomTheme" > ...
    

    【讨论】:

    • 感谢您的回答,我已经有一个自定义主题,但我错过了“searchAutoCompleteTextView”,但即使使用该项目,我似乎也无法更改背景颜色。也许有什么东西压倒了它。我必须深入挖掘
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多