【问题标题】:Change Toolbar overflow icon color更改工具栏溢出图标颜色
【发布时间】:2017-04-21 08:43:48
【问题描述】:

我的 Android 应用中有一个 android.support.v7.widget 工具栏。它的背景颜色是亮橙色,最好看的颜色是白色而不是黑色。

我的默认颜色是黑色而不是白色。由于它会与其他东西冲突,因此几乎不可能覆盖。 我无法将主要文本颜色更改为白色!

我已设法更改标题颜色。 我现在正在寻找的是如何将操作按钮的颜色也更改为白色。

我的代码:

主要活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".UI.activities.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/r2_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    app:titleTextColor="@color/primary_text_material_light"
    app:subtitleTextColor="@color/primary_text_material_light"
    android:theme="@style/R2Theme.Toolbar"/>

<fragment android:name="com.r2retail.r2retailapp.UI.fragments.SetupFragment"
    android:layout_below="@+id/r2_toolbar"
    android:id="@+id/list"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


</RelativeLayout>

菜单栏:

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

<item android:id="@+id/about"
    android:icon="@drawable/ic_menu"
    android:title="About"
    app:showAsAction="never"/>

</menu>

样式:

<resources>

<style name="R2Theme" parent="Theme.AppCompat.Light.NoActionBar">=
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/secondary_text_material_dark</item>
    <item name="android:textColorSecondaryInverse">@color/primary_text_material_light</item>
</style>

<style name="R2Theme.Toolbar" parent="R2Theme">
    <item name="actionMenuTextColor">@color/primary_text_material_light</item>
</style>

</resources>

【问题讨论】:

标签: java android android-layout android-view android-toolbar


【解决方案1】:

在样式方面:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="actionOverflowButtonStyle">@style/MyOverflowButtonStyle</item>
</style>

<style name="MyOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:tint">#62ff00</item>
</style>

结果:

【讨论】:

  • @azizbekian 谢谢,它有效,但如果我也想更改后退箭头(在子活动中)颜色怎么办?
  • @Shiva 欢迎。你试过this吗?
  • @azizbekian,是的,在那之后只有我发现了那个链接,是的,它有效,但在这里更新失败,非常感谢
  • 如果我使用 parent="Theme.AppCompat.Light.NoActionBar" 并将 AppTheme 应用于整个应用程序,则不起作用。
  • 也不适合我。我有一个 NoActionBar 主题应用于清单中的活动,但其余部分与上述相同。
【解决方案2】:

解决办法是更换图标本身。

第一

转到 values/styles 并在您的 styles.xml 文件中添加:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <!--Here you need to put name of drawable you will create during the next step-->
    <item name="android:src">@drawable/your_white_icon</item> 
</style>

第二次

然后转到 drawable 文件夹。鼠标右键单击-> 新建-> 矢量资产。 然后按图标图像并从名为ic_more_vert_black_24dp的建议图标中进行选择。

自定义 -> 按下一步 -> 完成。

然后打开新创建的图标文件。代码如下所示。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FFFFFFFF" <!-- Here u can change color-->
        android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>

fillColor 属性更改为您需要的颜色。将此文件放入第一步中描述的样式中。

瞧!我们的三个点的颜色改变不取决于基本应用样式(#FF2012 颜色的结果)。

【讨论】:

    【解决方案3】:

    另一种方式,用代码代替 XML:

    public static boolean colorizeToolbarOverflowButton(@NonNull Toolbar toolbar, @ColorInt int color) {
        final Drawable overflowIcon = toolbar.getOverflowIcon();
        if (overflowIcon == null)
            return false;
        toolbar.setOverflowIcon(getTintedDrawable(toolbar.getContext(), overflowIcon, toolbarIconsColor));
        return true;
    }
    
    public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) {
        Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
        DrawableCompat.setTint(wrapDrawable, color);
        DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN);
        return wrapDrawable;
    }
    

    如果成功为溢出图标着色,该函数将返回true。

    如果您不想使用有色可绘制对象,还有另一种选择:

    public static boolean colorizeToolbarOverflowButton(@NonNull Toolbar toolbar, @ColorInt Integer color) {
        final Drawable overflowIcon = toolbar.getOverflowIcon();
        if (overflowIcon == null)
            return false;
        final PorterDuffColorFilter colorFilter = toolbarIconsColor == null ? null : new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
        overflowIcon.setColorFilter(colorFilter);
        return true;
    }
    

    另外,如果你想给action item和nav item的图标上色,可以试试这个(基于here):

    /**
     * Use this method to colorize toolbar icons to the desired target color
     *
     * @param toolbarView       toolbar view being colored
     * @param toolbarIconsColor the target color of toolbar icons
     */
    @JvmStatic
    @UiThread
    fun colorizeToolbarActionItemsAndNavButton(toolbarView: Toolbar, @ColorInt toolbarIconsColor: Int?) {
        //https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color/
        val colorFilter = if (toolbarIconsColor == null) null else PorterDuffColorFilter(toolbarIconsColor, Mode.MULTIPLY)
        for (i in 0 until toolbarView.childCount) {
            val v = toolbarView.getChildAt(i)
            //Step 1 : Changing the color of back button (or open drawer button).
            if (v is ImageButton) {
                //Action Bar back button
                v.drawable.mutate().colorFilter = colorFilter
            }
            if (v is ActionMenuView) {
                for (j in 0 until v.childCount) {
                    //Step 2: Changing the color of any ActionMenuViews - icons that
                    //are not back button, nor text, nor overflow menu icon.
                    val innerView = v.getChildAt(j)
                    if (innerView is ActionMenuItemView) {
                        val drawablesCount = innerView.compoundDrawables.size
                        for (k in 0 until drawablesCount) {
                            if (innerView.compoundDrawables[k] != null) {
                                innerView.post { innerView.compoundDrawables[k].mutate().colorFilter = colorFilter }
                            }
                        }
                    }
                }
            }
        }
    }
    

    用法:

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        toolbar.doOnPreDraw {
            colorizeToolbarActionItemsAndNavButton(toolbar,0xffff0000.toInt())
        }
        return true
    }
    

    【讨论】:

      【解决方案4】:

      对于 AndroidX 用户(真的不知道它是否可以使用旧的支持库):

      TL;DR:

      <style name="MyToolbarTheme">
        <item name="colorControlNormal">@color/white</item>
      </style>
      

      MyToolbarTheme 应用于您的Toolbar 视图。


      长解释:

      Widget.AppCompat.ActionButton.Overflow 扩展 Base.Widget.AppCompat.ActionButton.Overflow。我们将讨论后者:

      关于默认实现:

      <style name="Base.Widget.AppCompat.ActionButton.Overflow" parent="RtlUnderlay.Widget.AppCompat.ActionButton.Overflow">
        <item name="srcCompat">@drawable/abc_ic_menu_overflow_material</item>
        ...
      </style>
      

      关于 API 21 实现:

      <style name="Base.Widget.AppCompat.ActionButton.Overflow" parent="android:Widget.Material.ActionButton.Overflow">
        <item name="android:src">@null</item>
        <item name="srcCompat">@drawable/abc_ic_menu_overflow_material</item>
      </style>
      

      在 API 23 及更高版本上:

      它扩展了android:Widget.Material.ActionButton.Overflow

      <style name="Widget.Material.ActionButton.Overflow">
        <item name="src">@drawable/ic_menu_moreoverflow_material</item>
        ...
      </style>
      

      我们可以注意到每个实现都使用@drawable/ic_menu_moreoverflow_material

      在这个可绘制对象的实现中,您可以看到以下内容:

      android:tint="?attr/colorControlNormal"
      

      【讨论】:

        【解决方案5】:

        如果您想更改工具栏中图标(导航图标、菜单项图标)的颜色,只需使用以下代码即可。我已经保存了问题并使用它解决了。

        <!--Light Theme-->
        <style name="AppThemeLight" parent="Theme.MaterialComponents.Light.NoActionBar">
            <!--other colors and properties-->
            <item name="iconTint">@color/colorBlack</item>
        </style>
        
        <!-- Dark/Night theme. -->
        <style name="AppThemeDark" parent="Theme.MaterialComponents.NoActionBar">
            <!--other colors and properties-->
            <item name="iconTint">@color/colorWhite</item>
        </style>
        

        【讨论】:

          【解决方案6】:

          如果有人希望以编程方式更改它,但以下方法不起作用:

          mBinding.toolbar.overflowIcon?.setTint(Color.WHITE)
          

          mBinding.toolbar.overflowIcon?.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
          

          mBinding.toolbar.overflowIcon?.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.WHITE, BlendModeCompat.SRC_ATOP)
          

          val overflowIcon = ContextCompat.getDrawable(this, R.drawable.dots_vertical_black)
          overflowIcon?.setTint(Color.WHITE)
          mBinding.toolbar.overflowIcon = overflowIcon
          

          试试这个。

          最后,下面这行代码对我有用(经过 2 天的反复试验-_-)

          mBinding.toolbar.menu?.findItem(R.id.menu)?.icon?.setTint(Color.WHITE)
          

          【讨论】:

            猜你喜欢
            • 2015-03-15
            • 2020-09-30
            • 1970-01-01
            • 1970-01-01
            • 2019-03-11
            • 2021-07-19
            • 1970-01-01
            • 1970-01-01
            • 2015-04-21
            相关资源
            最近更新 更多