4

当我使用以下配置时:

<item
    android:id="@+id/menu_item_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"        
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom"
    android:title="@string/action_search"/>

ActionBarSherlock SearchView 对提供的 android:icon 没有反应

但万一

android:showAsAction="ifRoom|collapseActionView"

图标已正确更改,但搜索视图内的关闭按钮不存在。

如何更改搜索图标并在右侧搜索视图角落显示关闭按钮?

这是我的应用主题:

<style name="AppTheme" parent="AppBaseTheme">               
    <item name="android:windowBackground">@color/background_window</item>
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <item name="homeAsUpIndicator">@drawable/new_indicator</item>
    <item name="android:homeAsUpIndicator">@drawable/new_indicator</item>

    <!-- ActionBar (SearchView) fix -->
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
    <item name="searchDropdownBackground">@drawable/abs__search_dropdown_dark</item>
    <item name="searchViewTextField">@drawable/abs__textfield_searchview_holo_dark</item>
    <item name="searchViewTextFieldRight">@drawable/abs__textfield_searchview_right_holo_dark</item>        
    <item name="searchViewCloseIcon">@drawable/abs__ic_clear</item>
    <item name="searchViewSearchIcon">@drawable/abs__ic_voice_search</item>
    <item name="searchViewGoIcon">@drawable/abs__ic_go</item>
    <item name="searchViewVoiceIcon">@drawable/abs__ic_voice_search</item>
    <item name="searchAutoCompleteTextView">@style/Widget.Sherlock.SearchAutoCompleteTextView.DarkActionBar</item>         
</style>

<style name="Widget.Sherlock.SearchAutoCompleteTextView.DarkActionBar">
    <item name="android:textColor">?attr/textColorPrimaryInverse</item>
</style>

来自清单的应用程序:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

操作栏菜单:

<item
    android:id="@+id/menu_item_search"
    android:icon="@drawable/abs__ic_clear"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"        
    android:showAsAction="ifRoom"
    android:title="@string/action_search"/>    
<item
    android:id="@+id/menu_item_contact"
    android:icon="@drawable/ic_action_user"
    android:showAsAction="ifRoom"
    android:title="@string/action_contact"/>  

SearchView 菜单配置:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();

    if (null != searchView) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);         
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            // adapter.getFilter().filter(newText);
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            // this is your adapter that will be filtered
            // adapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
4

1 回答 1

7

您可以通过为活动主题设置属性来完成此操作。在您的 styles.xml 中添加以下行,并确保将 MyTheme 或您想要调用的任何内容应用于您的应用程序或搜索视图所在的活动。我还添加了“searchAutoCompleteTextView”属性,它允许您自定义文本颜色等,如果您愿意的话。

    <style name="MyTheme" parent="Theme.Sherlock">
            <item name="searchViewCloseIcon">@drawable/YOUR_CLOSE_ICON</item>
            <item name="searchViewSearchIcon">@drawable/YOUR_SEARCH_ICON</item>
            <item name="searchAutoCompleteTextView">@style/searchTextView</item>
    </style>

    <style name="searchTextView" parent="Widget.Sherlock.SearchAutoCompleteTextView">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textColorHint">@android:color/black</item>
    </style>
于 2013-06-06T20:22:48.260 回答