简体   繁体   English

如何更改SearchView Android的搜索图标

[英]How to change the search icon of searchview android

I want to change the default search icon of searchview. 我想更改searchview的默认搜索图标。 I have tried the below things : 我已经尝试了以下方法:

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

But It is not changing the search icon at all, how can I do this. 但这根本没有改变搜索图标,我该怎么做。 I have checked other links also and tried the solutions given but that also not working in my case. 我也检查了其他链接,并尝试了给出的解决方案,但在我的情况下也不起作用。

How to change the default icon on the SearchView, to be use in the action bar on Android? 如何更改要在Android的操作栏中使用的SearchView上的默认图标?

Please help if anyone have idea here. 如果有人在这里请帮忙。

Make a menu.xml 制作一个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>

You can specify any drawable of your choice in the android:icon property 您可以在android:icon属性中指定任意选择的可绘制对象

in the activity, inflate the menu 在活动中,增加菜单

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

} }

<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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM