如何更改searchview android的搜索图标

时间:2016-03-22 05:32:03

标签: android searchview

我想更改searchview的默认搜索图标。 我尝试过以下的事情:

int searchImgId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
    ImageView searchIconView = (ImageView) mSearchView.findViewById(searchImgId);
    if(searchIconView != null){
        searchIconView.setImageResource(R.drawable.ic_clear_white);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        searchIconView.setLayoutParams(layoutParams);

但它根本没有改变搜索图标,我该怎么做呢。我也检查了其他链接并尝试了解决方案,但在我的情况下也没有用。

How to change the default icon on the SearchView, to be use in the action bar on Android?

如果有人在这里有想法,请帮助。

2 个答案:

答案 0 :(得分:0)

制作menu.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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".screens.MoviesScreen">

    <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/search"
        app:showAsAction="always" />    

</menu>

您可以在android:icon属性

中指定您选择的任何drawable

在活动中,膨胀菜单

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

}

答案 1 :(得分:0)

<android.support.v7.widget.SearchView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    app:searchIcon="@drawable/my_search_icon"
    app:voiceIcon="@drawable/my_voice_search_icon"
    app:closeIcon="@drawable/my_close_icon"
    app:commitIcon="@drawable/my_commit_icon"
    app:goIcon="@drawable/my_go_icon"
    app:searchHintIcon="@drawable/my_search_hint_icon"/>