【问题标题】:AppCompat Toolbar: Change Overflow Icon Color in ActionModeAppCompat 工具栏:在 ActionMode 中更改溢出图标颜色
【发布时间】:2015-03-15 17:40:59
【问题描述】:

使用 AppCompat 工具栏,我希望能够在 ActionMode 更改时更改溢出菜单图标的颜色。

例如,在正常工具栏模式下,溢出图标将是白色的。并且会在 ActionMode 上变黑。到目前为止,我已经设法改变了动作模式的背景以及标题文本。但是我还没有找到改变溢出菜单图标颜色的方法。

我知道有一个可用的答案: Change ActionMode Overflow icon

我尝试了第一个解决方案,但找不到溢出图标。

第二种解决方案,即使有 50L 延迟,也会导致溢出菜单图标在很短的瞬间闪烁 ActionMode 的预期颜色,这非常刺耳。

【问题讨论】:

    标签: android overflow toolbar android-appcompat actionmode


    【解决方案1】:

    将以下行添加到您的主题属性中:

    <item name="android:textColorSecondary">@android:color/white</item>
    

    【讨论】:

    • 我不知道我还有第二种解决方案,请查看此链接stackoverflow.com/questions/26671677/…
    • 当心,这也会改变某些设备底部菜单(通过按特殊键显示)中的文本颜色。如果textColorSecondary 和菜单背景设置为相同(或相似)的颜色,这可能是个问题。
    • Bette 的方法是为它创建一个单独的样式:&lt;style name="ToolbarTheme" parent="AppTheme.MyTheme"&gt; &lt;item name="android:textColorSecondary"&gt;@android:color/white&lt;/item&gt; &lt;/style&gt;
    • 这不会导致动作模式和普通工具栏的颜色不同。
    【解决方案2】:

    这可以通过设置android:textColorSecondary主题属性来实现。

    例如,假设您有以下工具栏,它使用主题MyToolbarStyle

    <android.support.v7.widget.Toolbar
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?attr/actionBarSize"
      theme="@style/MyToolbarStyle"
    />
    

    接下来,定义样式MyToolbarStyle,继承自ThemeOverlay.AppCompat.ActionBar。最后,修改溢出图标的颜色,为android:textColorSecondary添加一项:

    <style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.ActionBar">
      <item name="android:textColorSecondary">#333333</item>
    </style>
    

    【讨论】:

    • 嗨。感谢您的回答...但问题是溢出图标菜单颜色由 ActionMode 的溢出图标和工具栏的溢出图标共享。目标是为两者使用不同的颜色。
    【解决方案3】:
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
         <item name="android:actionOverflowButtonStyle">@style/ActionButton.Overflow.Icon</item>
     </style>
    
     <style name="ActionButton.Overflow.Icon" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
         <item name="android:src">@mipmap/yourwanticon</item>
     </style>
    

    【讨论】:

    • 唯一的答案是完美的,不会改变文本颜色或菜单项颜色 - 只有溢出菜单图标。
    【解决方案4】:
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
    
        <!-- Support library compatibility -->
        <item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
    </style>
    
    <style name="ActionButtonOverflow" parent="@style/Widget.AppCompat.ActionButton.Overflow">
        <item name="android:tint">@color/brand_white</item>
    </style>
    

    【讨论】:

      【解决方案5】:

      将此代码添加到您的 res->styles.xml

      <style name="ToolbarColored" parent="AppTheme">
      <item name="android:textColorSecondary">YOUR_COLOR</item>
      </style>
      

      然后你的 XML 文件中的“ToolbarColored”样式就像 belove

      <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              app:theme="@style/ToolbarColored"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" />
      

      【讨论】:

        【解决方案6】:

        要正确更改工具栏溢出菜单图标的颜色,请将工具栏的主题设置为 AppCompat 深色 ActionBar 主题。例如:

        在您的 res/values/style.xml 文件中创建一个以这种方式从 AppCompat 继承的主题:

        <style name="AppTheme.MyThemeName" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
        

        现在将工具栏的主题设置为此主题:

        <android.support.v7.widget.Toolbar
          android:id="+id/my_toolbar_id
          android:layout_width="match_parent"
          android:layout_height="@dimen/my_toolbar_height"
          android:theme="@style/AppTheme.MyThemeName">
        
        </android.support.v7.widget.Toolbar>
        

        【讨论】:

          【解决方案7】:
          <style name="ToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
              <item name="android:tint">@color/colorAccent</item>
          

          创建上述主题。使用您的颜色设置色调并将此主题添加到工具栏。

          <androidx.appcompat.widget.Toolbar
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:minHeight="?attr/actionBarSize"
                  android:theme="@style/ToolBarTheme"/>
          

          【讨论】:

          • 迄今为止最好的解决方案。
          【解决方案8】:

          这里没有一个答案可以帮助我独立于普通工具栏的溢出图标颜色更改 ActionMode 的溢出图标颜色(无需在代码中使用逐个案例样式)。经过一些试验和错误,我认为我们可以独立于 Toolbar 覆盖 ActionMode 的 theme 属性,并且成功了!

          在基本主题中,我们像往常一样指定动作模式的样式:

          <style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.Bridge">
                  <item name="actionModeStyle">@style/ActionModeStyle</item>
          </style>
          

          在我们自定义的ActionModeStyle 中,我们可以做任何我们想要的样式,并指定一个theme 属性:

          <style name="ActionModeStyle" parent="@style/Widget.AppCompat.ActionMode">
                  <item name="theme">@style/ActionMode.Theme</item>
          </style>
          
          <style name="ActionMode.Theme" parent="ThemeOverlay.AppCompat.Dark">
                  <item name="android:textColorSecondary">?attr/colorPrimary</item>
          </style>
          

          textColorSecondary 也会改变后退按钮的颜色,但我们可以使用actionModeCloseButtonStyle 轻松覆盖那个颜色。

          【讨论】:

            【解决方案9】:

            首先制作你的自定义样式

             <style name="ToolbarColoredBackArrow" parent="AppTheme">
                <item name="android:textColorSecondary">@color/white</item>
             </style>
            

            然后将其添加到您的工具栏

                 <android.support.v7.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:titleTextColor="@color/white"
                      app:theme="@style/ToolbarColoredBackArrow"
                      android:layout_height="?attr/actionBarSize"
                      app:layout_scrollFlags="scroll|enterAlways"
                      android:background="?attr/colorPrimary" />
            

            【讨论】:

              【解决方案10】:

              如果您在活动 xml 中使用工具栏,则可以使用类似的东西

              toolbar?.navigationIcon?.setColorFilter(ContextCompat.getColor(this, android.R.color.black), PorterDuff.Mode.SRC_ATOP)
              

              【讨论】:

              • 谢谢。这对我帮助很大。如果菜单项包含actionLayout怎么办?
              【解决方案11】:

              如果您想要 白色 溢出菜单图标,只需添加 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 到您的工具栏布局代码。

              如果您想要 dark 溢出菜单图标,请使用 android:theme="@style/Base.Widget.AppCompat.Light.PopupMenu"

              所以最终的代码是这样的:

              <android.support.v7.widget.Toolbar
                      android:id="@+id/a_main_tb"
                      android:layout_width="match_parent"
                      android:layout_height="?actionBarSize"
                      android:background="@color/colorPrimary"
                      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                      app:title="@string/app_name"
                      app:titleTextColor="#ffffff"
                      />
              

              另外,你应该明白它也会改变菜单项的颜色。

              【讨论】:

                【解决方案12】:
                <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                    <item name="actionOverflowButtonStyle">@style/CustomOverflowButtonStyle</item>
                </style>
                <style name="CustomOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
                    <item name="android:tint">@color/black</item>
                </style>
                

                【讨论】:

                • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
                猜你喜欢
                • 1970-01-01
                • 2014-12-20
                • 1970-01-01
                • 2020-09-30
                • 2015-05-16
                • 1970-01-01
                • 2019-03-11
                • 1970-01-01
                相关资源
                最近更新 更多