每当我输入搜索视图时,关闭按钮(X)似乎由于某种原因而被禁用,而不是以白色显示。如何解决此错误以使其显示为白色?
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_search_list, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
searchView.setQueryHint(getResources().getString(R.string.station_search_hint));
}
menu_search_list.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search_light"
android:title="@string/action_search"
android:orderInCategory="100"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
答案 0 :(得分:3)
清空解决方案时隐藏关闭图标。
在 app:showAsAction 属性中使用&#34; collapseActionView | always&#34; 。
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/menu_action_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
答案 1 :(得分:1)
ImageView close_button = (ImageView)searchView.findViewById(R.id.close_button);
close_button .setBackgroundResource(R.drawable.your_icon);
或(使用选择器)
ImageView close_button =(ImageView)searchView.findViewById(R.id.close_button);
close_button .setBackgroundResource(R.drawable.your_image_selector);
答案 2 :(得分:1)
我认为下一代码的复制粘贴:
View close = searchView.findViewById(R.id.search_close_btn);
close.setBackgroundResource(R.drawable.desired_icon);
不是好的解决方案。请使用样式。
第一个解决方案。在主要主题中添加关闭图标作为默认android:searchViewCloseIcon
:
<style name="Theme" parent="AppBaseTheme">
<item name="android:searchViewCloseIcon">@drawable/ic_your_search_close_icon</item>
</style>
第二种解决方案(更好)。设置操作栏的样式
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewCloseIcon">@android:drawable/ic_your_search_close_icon</item>
</style>
其他SearchView属性(来自appcompat-v7 source code(更新的链接)):
<attr name="searchResultListItemHeight" format="dimension" />
<attr name="searchViewAutoCompleteTextView" format="reference" />
<attr name="searchViewCloseIcon" format="reference" />
<attr name="searchViewEditQuery" format="reference" />
<attr name="searchViewEditQueryBackground" format="reference" />
<attr name="searchViewGoIcon" format="reference" />
<attr name="searchViewSearchIcon" format="reference" />
<attr name="searchViewTextField" format="reference" />
<attr name="searchViewTextFieldRight" format="reference" />
<attr name="searchViewVoiceIcon" format="reference" />
Exaple用法:
<item name="searchDropdownBackground">@android:drawable/spinner_dropdown_background</item>
<item name="searchViewTextField">@drawable/textfield_searchview_holo_dark</item>
<item name="searchViewTextFieldRight">@drawable/textfield_searchview_right_holo_dark</item>
<item name="searchViewCloseIcon">@android:drawable/ic_clear</item>
<item name="searchViewSearchIcon">@android:drawable/ic_search</item>
<item name="searchViewGoIcon">@android:drawable/ic_go</item>
<item name="searchViewVoiceIcon">@android:drawable/ic_voice_search</item>
<item name="searchViewEditQuery">@android:drawable/ic_commit_search_api_holo_dark</item>
<item name="searchViewEditQueryBackground">?attr/selectableItemBackground</item>
<item name="searchDialogTheme">@style/Theme.SearchBar</item>
有关造型的更多信息:
Android – Theming ActionBar and the SearchView within it作者:Andreas Nilsson
Android官方文档Styling the Action Bar
答案 3 :(得分:1)
您的代码似乎很好。 试试下面的代码:
final MenuItem search = menu.findItem(R.id.action_search);
if (search != null) {
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
search.expandActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Your Logic
return false;
}
});
MenuItemCompat.setOnActionExpandListener(search, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
finish();
return true;
}
});
再调整一次:
在onPrepareOptionsMenu
中尝试相同的上述代码,并在顶部添加以下额外行
menu.clear();
getMenuInflater().inflate(R.menu.menu_search_list, menu);
...
// Above code