【问题标题】:How to customize the search icon on toolbar in android?如何在android中自定义工具栏上的搜索图标?
【发布时间】:2016-08-14 07:01:04
【问题描述】:
<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>

我想把右上角的search image改成黑色,我想把它改成橙色

【问题讨论】:

  • 目前是黑色的。这是一张黑色的图像。您必须创建一个颜色为橙色的图像。并替换它!!!
  • 已经试过了,但是不行
  • 请发布您的代码以获得更好的答案
  • 黑色图片是默认图片,我已经在menu.xml文件中添加了橙色图片。但仍然显示黑色图像。

标签: android


【解决方案1】:
@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);
        }

在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>

【讨论】:

    【解决方案2】:

    您可以通过 setColorFilter 重置 MenuItem 的颜色。

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

    【讨论】:

      【解决方案3】:

      onCreateOptionsMenu(Menu menu, MenuInflater inflater) 你可以使用这个:
      注意:您可能想使用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);
      

      但就我个人而言,我可能只是手动更改可绘制对象的颜色(即用彩色替换可绘制对象)。

      【讨论】:

        【解决方案4】:

        在您的菜单项中使用下面的 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" />
        

        // 请确保您在活动或片段中使用相同的菜单项 xml。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-13
          相关资源
          最近更新 更多