简体   繁体   English

如何自定义Android工具栏上的搜索图标?

[英]How to customize the search icon on toolbar in android?

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="ifRoom"
        android:title="Search"/>
</menu>

I want to change the search image in the top right corner, currently it is black and I want to change it to orange 我想更改右上角的搜索图像 ,当前它是黑色,我想将其更改为橙色

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_notifications, menu);
            this.menu = menu;
            super.onCreateOptionsMenu(menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_clear_all) {
                showToast("Clear All");
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

Create xml file under res>menu. 在res> menu下创建xml文件。

<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=".YourActivityName" >

    <item
        android:id="@+id/action_clear_all"
        android:title="@string/noti_clear_all"
        android:icon="@drawable/ic_clear_all"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />

</menu>

You can reset the color of your MenuItem by setColorFilter . 您可以通过setColorFilter重置MenuItem的颜色。

Drawable can be changed to any color. Drawable可以更改为任何颜色。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_menu_frag, menu);
    // replace MenuItem with your own
    MenuItem item = menu.getItem(0);
    final Drawable drawable = item.getIcon();
    if (drawable != null) {
        final int color = Color.RED;
        drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    }
}

In onCreateOptionsMenu(Menu menu, MenuInflater inflater) you can use this: onCreateOptionsMenu(Menu menu, MenuInflater inflater)您可以使用以下命令:
Note: You may want to use PorterDuff.Mode.SRC_IN instead of PorterDuff.Mode.MULTIPLY ) 注意:您可能要使用PorterDuff.Mode.SRC_IN而不是PorterDuff.Mode.MULTIPLY

MenuItem searchItem = menu.findItem(R.id.action_search);
int color = 0; // SPECIFY THE COLOUR YOU WANT HERE. I suggest you retrieve colour from colors.xml
menuItem.getIcon().setColorFilter(color, PorterDuff.Mode.MULTIPLY);

But personally I would probably just change the colour of the drawable manually (ie replace the drawable with a coloured one). 但是我个人可能只是手动更改可绘制对象的颜色(即用彩色对象替换可绘制对象)。

Use below XML in your menu item 在菜单项中使用以下XML

<item
        android:id="@+id/menu_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/search"
        android:showAsAction="ifRoom|collapseActionView"
        android:title="@string/menu_search" />

// Please make sure you using same menu item xml in your activity or fragment. //请确保您在活动或片段中使用相同的菜单项xml。

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

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