当我使用以下配置时:
<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"
图标已正确更改,但搜索视图中的关闭按钮不存在。
如何更改搜索图标并在右侧搜索视图角显示关闭按钮?
这是我的AppTheme:
<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);
答案 0 :(得分: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>