当我使用以下配置时:
<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);