How can i change the icon color of searchview in actionbar?

后端 未结 2 1110
自闭症患者
自闭症患者 2021-01-24 06:42

This is my menu.xml



        
相关标签:
2条回答
  • 2021-01-24 07:02

    I found another solution to override the default searchview icons color so you don't have to use a drawable.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.action_search).getActionView();
        ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
        icon.setColorFilter(Color.WHITE);
        return super.onCreateOptionsMenu(menu);
    }
    

    This code changes the color of the default searchview icon.

    android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.action_search).getActionView();
    ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    icon.setColorFilter(Color.WHITE);
    
    0 讨论(0)
  • 2021-01-24 07:18

    You need to use android.support.v7.widget.SearchView

    <style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
        <!-- Gets rid of the search icon -->
        <item name="searchIcon">@drawable/ic_search</item>
        <!-- Gets rid of the "underline" in the text -->
        <item name="queryBackground">@color/white</item>
        <!-- Gets rid of the search icon when the SearchView is expanded -->
        <item name="searchHintIcon">@null</item>
        <!-- The hint text that appears when the user has not typed anything -->
        <item name="queryHint">@string/search_hint</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题