【问题标题】:Change the color of overflow menu icons in a material toolbar更改材质工具栏中溢出菜单图标的颜色
【发布时间】:2020-09-30 10:43:10
【问题描述】:

我想根据android:theme 值更改材质工具栏中的溢出菜单图标颜色。

在此示例中,我的搜索图标通过与 app:actionViewClass 属性实现相关的一些魔法受到工具栏主题的影响,但我的信息图标没有。

我的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:liftOnScroll="true">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        app:title="@string/app_name"
        app:menu="@menu/menu_main"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

</com.google.android.material.appbar.AppBarLayout>

我的菜单:

<?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/action_search"
        android:icon="@drawable/ic_search_24dp"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        android:title="TODO" />

    <item android:id="@+id/action_info"
        android:icon="@drawable/ic_info_24dp"
        app:showAsAction="always"
        android:title="TODO" />

</menu>

是否存在简单的解决方案?

【问题讨论】:

    标签: android android-toolbar material-components-android material-components


    【解决方案1】:

    你可以使用:

    <com.google.android.material.appbar.MaterialToolbar
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        android:theme="@style/MyThemeOverlay_Toolbar"
        ...>
    
      <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/myColor</item>
      </style>
    

    【讨论】:

    • 我在很多类似的问题中都看到过这个答案,但它实际上对我不起作用,这在您的机器上是否符合我们的预期?
    • @Fabio 您使用的是哪个版本的 Material Components?
    • 稳定版 1.1.0 - 编辑:刚刚尝试使用 1.3.0-alpha01,行为相同
    • @Fabio 刚刚使用1.1.01.2.0-beta011.3.0-alpha01 在一个干净的项目中进行了尝试。有用。注意主题叠加层中的parent="ThemeOverlay.MaterialComponents.Toolbar.Primary"
    • 奇怪,你也是用android studio 4.0版本的吗?
    【解决方案2】:

    您可以前往 Material Components 文档,其中包含大量关于样式库中视图的每个部分的信息。

    在这种情况下,动作图标的色调似乎是由colorControlNormal 属性设置的。

    文档 - Top app bars

    【讨论】:

      【解决方案3】:

      也许这个答案来得太晚了,但以防其他人需要它,我会在这里发布我的答案。

      如果您将属性 theme 设置为 AppBarLayout 而不是 MaterialToolbar,您所面临的问题将得到解决。

      因此,在您的代码中,您的工具栏应如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <com.google.android.material.appbar.AppBarLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:fitsSystemWindows="true"
          android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
          app:liftOnScroll="true">
      
          <com.google.android.material.appbar.MaterialToolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              style="@style/Widget.MaterialComponents.Toolbar.Primary"
              app:title="@string/app_name"
              app:menu="@menu/menu_main"
              app:layout_scrollFlags="scroll|enterAlways|snap"/>
      
      </com.google.android.material.appbar.AppBarLayout>
      

      希望这对任何人都有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-15
        • 1970-01-01
        • 2017-03-02
        • 2017-05-20
        • 1970-01-01
        • 2021-11-08
        相关资源
        最近更新 更多