簡體   English   中英

如何更改SearchView Android的搜索圖標

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

我想更改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);

但這根本沒有改變搜索圖標,我該怎么做。 我也檢查了其他鏈接,並嘗試了給出的解決方案,但在我的情況下也不起作用。

如何更改要在Android的操作欄中使用的SearchView上的默認圖標?

如果有人在這里請幫忙。

制作一個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屬性中指定任意選擇的可繪制對象

在活動中,增加菜單

@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