在searchview中自定义关闭图标

时间:2015-06-04 20:36:25

标签: android android-actionbar searchview

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

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

3 个答案:

答案 0 :(得分:8)

如果你正在使用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/

答案 1 :(得分:0)

这对我有用:

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;
}

答案 2 :(得分:0)

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

app:closeIcon="@drawable/ic_search_close"