搜索视图关闭图标显示为禁用而不是白色
Posted
技术标签:
【中文标题】搜索视图关闭图标显示为禁用而不是白色【英文标题】:Search view close icon appearing disabled rather than white 【发布时间】:2015-09-14 14:42:17 【问题描述】:每当我在搜索视图中输入内容时,关闭按钮 (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>
【问题讨论】:
【参考方案1】:清除为空时隐藏关闭图标的解决方案。
在 app:showAsAction 属性中使用 "collapseActionView|always"。
<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"/>
【讨论】:
【参考方案2】:您的代码看起来不错。 试试下面的代码:
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
【讨论】:
【参考方案3】: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);
【讨论】:
你知道如何用正确的答案和代码解决这个问题吗?Fast scroll large letter thumb preview not appearing 我们可以设置分配给关闭按钮的空间吗?我使用上面的代码它工作正常,但我设置的新图像几乎与搜索框的中心对齐【参考方案4】:我想复制粘贴下一个代码:
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" />
示例用法:
<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 by Andreas NilssonStyling the Action Bar by Android 官方文档
【讨论】:
@MacaronLover,尝试删除前缀android:
。只留下searchViewCloseIcon
我现在收到此错误:Cannot resolve symbol 'android:searchViewCloseIcon'
。我该如何解决?
你的styles.xml
:<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView"> <item name="searchViewCloseIcon">@drawable/ic_up</item> </style>
在你的主题中:<style name="Theme" parent="FrameworkRoot.Theme"> <item name="android:searchViewStyle">@style/SearchViewStyle</item> </style>
错误现在消失了,但这是否也需要放入 styles.xml 中? <attr name="searchViewCloseIcon" format="reference" />
当然这应该在styles.xml而不是attrs.xml以上是关于搜索视图关闭图标显示为禁用而不是白色的主要内容,如果未能解决你的问题,请参考以下文章