1

我使用下面的代码不起作用:错误:错误:未找到与给定名称匹配的资源:attr 'searchViewCloseIcon'。如何更改 searchview 中的关闭图标。我非常感谢任何帮助。提前致谢。我也看到此链接Android SearchView X 标记图标但无法实现它。而且它不是仅操作栏的搜索视图

在 values 文件夹中的 styles.xml

<style name="Theme" parent="AppBaseTheme">
<item name="android:searchViewCloseIcon">@android:drawable/ic_search_close</item>
</style>

我试过这段代码:

        int linlayId = getResources().getIdentifier("android:id/search_plate", null, null);
        ViewGroup v = (ViewGroup) src.findViewById(linlayId);
        v.setBackgroundResource(R.drawable.ic_launcher);

上面的一个有效,但下面的一个无效。

        int linlayId = getResources().getIdentifier("android:id/search_close_btn", null, null);
        ViewGroup v = (ViewGroup) src.findViewById(linlayId);
        v.setBackgroundResource(R.drawable.ic_launcher);
4

3 回答 3

4

search_close_btn 是一个图像视图,设置它的背景不适用于这个。尝试以下操作:

 int closeButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
        ImageView closeButton = (ImageView) searchView.findViewById(closeButtonId);
        closeButton.setImageResource(R.drawable.mydrawable);
于 2014-08-26T13:18:37.150 回答
2

刚刚找到了 Appcompat Searchview 十字图标的随机解决方法

 app:closeIcon="@drawable/delete"

希望它可以帮助某人。

于 2016-01-15T07:14:29.047 回答
1

如果你有 AppCompat SearchView,你必须设置 searchViewCloseIcon 没有“android”

<item name="android:searchViewCloseIcon">@android:drawable/ic_search_close</item>
于 2014-01-13T12:29:41.857 回答