自定义搜索视图中的关闭图标

Customize close icon in searchview

如何更改searchview 的closeIcon 的资源。使用 Holo.light 作为 parent。我尝试了很多东西!这个名字不在 Holo 上!!

<item name="android:searchViewCloseIcon">@drawable/ic_search_close</item>

如果您使用的是 AppCompat v21 compile 'com.android.support:appcompat-v7:22.2.0',您可以使用这个:

values/themes.xml:
<style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
    <item name=”searchViewStyle”>@style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">...</item>
    <!-- Background for the actions section (e.g. voice, submit) -->
    <item name="submitBackground">...</item>
    <!-- Close button icon -->
    <item name="closeIcon">...</item>
    <!-- Search button icon -->
    <item name="searchIcon">...</item>
    <!-- Go/commit button icon -->
    <item name="goIcon">...</item>
    <!-- Voice search button icon -->
    <item name="voiceIcon">...</item>
    <!-- Commit icon shown in the query suggestion row -->
    <item name="commitIcon">...</item>
    <!-- Layout for query suggestion rows -->
    <item name="suggestionRowLayout">...</item>
</style>

在此处找到:http://android-developers.blogspot.nl/2014/10/appcompat-v21-material-design-for-pre.html

如果你想要一致性,我建议使用这个库。

如果你真的不想使用这个库你可以用这个来改变关闭图标:

int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);  
ImageView closeButtonImage = (ImageView) searchView.findViewById(closeButtonId);  
closeButtonImage.setImageResource(R.drawable.ic_action_cancel);  

从这里得到:http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/

这对我有用:

private void changeCloseIcon(SearchView searchView) {    
    ImageView closeButtonImage = searchView.findViewById(R.id.search_close_btn);
    closeButtonImage.setImageResource(R.drawable.icon_toolbar_close);
}

 @Override
 public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    changeCloseIcon(searchView);      
    return true;
}

如果您使用的是 androidx.appcompat.widget.SearchView,您还可以在布局中对其进行自定义 XML:

app:closeIcon="@drawable/ic_search_close"

在 Kotlin 中使用如下。在onCreateOptionsMenu方法中处理它很重要。

   import androidx.appcompat.widget.SearchView


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.main_menu, menu)

    val searchItem = menu.findItem(R.id.action_search)
    val searchView: SearchView = searchItem.actionView as SearchView
    val closeIcon = searchView.findViewById<ImageView>(R.id.search_close_btn)
    closeIcon.setBackgroundColor(
        ContextCompat.getColor(
            requireContext(),
            R.color.radio_tab_text_color
        )
    )
}

用于实现可绘制文件;

closeIcon.setBackgroundResource(R.drawable.ic_close)

注意:检查是否导入了androidx.appcompat.widget.SearchView