简体   繁体   English

AppCompat 工具栏:在 ActionMode 中更改溢出图标颜色

[英]AppCompat Toolbar: Change Overflow Icon Color in ActionMode

With the AppCompat Toolbar, I want to be able to change the color of the overflow menu icon on ActionMode change.使用 AppCompat 工具栏,我希望能够在 ActionMode 更改时更改溢出菜单图标的颜色。

For example, the overflow icon will be white in normal Toolbar mode.例如,在正常工具栏模式下,溢出图标将为白色。 And will turn black on ActionMode.并且会在 ActionMode 上变黑。 So far, I have managed to change the background of the action mode as well as the title text.到目前为止,我已经设法改变了动作模式的背景以及标题文本。 But I have yet to find a way to change the overflow menu icon color.但我还没有找到改变溢出菜单图标颜色的方法。

I know that there's an answer available: Change ActionMode Overflow icon我知道有一个可用的答案: 更改 ActionMode 溢出图标

I tried the first solution and I wasn't able to find the overflow icon.我尝试了第一个解决方案,但找不到溢出图标。

The second solution, even with a 50L delay causes the overflow menu icon to flash the ActionMode's intended color for a brief split second that is very jarring.第二种解决方案,即使有 50L 的延迟,也会导致溢出菜单图标在非常刺耳的短暂瞬间闪烁 ActionMode 的预期颜色。

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

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

This can be achieved by setting the android:textColorSecondary theme attribute.这可以通过设置android:textColorSecondary主题属性来实现。

For example, suppose you have the following toolbar, which uses the theme MyToolbarStyle :例如,假设您有以下工具栏,它使用主题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"
/>

Next, define the style MyToolbarStyle , inheriting from ThemeOverlay.AppCompat.ActionBar .接下来,定义样式MyToolbarStyle ,继承自ThemeOverlay.AppCompat.ActionBar Finally, modify the color of the overflow icon by adding an item for android:textColorSecondary :最后,通过为android:textColorSecondary添加一个项目来修改溢出图标的颜色:

<style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.ActionBar">
  <item name="android:textColorSecondary">#333333</item>
</style>
<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>
<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>

Add this code on your res->styles.xml将此代码添加到 res->styles.xml

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

Then your 'ToolbarColored' style in your XML file like belove然后你的 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" />

To correctly change the color of your toolbar's overflow menu icon, set your toolbar's theme to an AppCompat dark ActionBar theme.要正确更改工具栏溢出菜单图标的颜色,请将工具栏的主题设置为 AppCompat 深色 ActionBar 主题。 For example:例如:

In your res/values/style.xml file create a theme that inherits from AppCompat in this manner:在您的 res/values/style.xml 文件中创建一个以这种方式从 AppCompat 继承的主题:

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

Now set your toolbar's theme to this theme:现在将工具栏的主题设置为此主题:

<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>
<style name="ToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:tint">@color/colorAccent</item>

create the above theme.set tint with your color and add this theme to the toolbar.使用您的颜色创建上面的 theme.set tint 并将此主题添加到工具栏。

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

None of the answers here helped me change the overflow icon color of ActionMode independently from the overflow icon color of the normal Toolbar (without resorting to case-by-case styling in code).这里的任何答案都没有帮助我独立于正常工具栏的溢出图标颜色更改 ActionMode 的溢出图标颜色(无需在代码中采用逐案样式)。 After some trial and error, I thought that we might override theme attribute of ActionMode independently from Toolbar, and it worked!经过一些试验和错误,我认为我们可以独立于 Toolbar 覆盖 ActionMode 的theme属性,并且成功了!

In the base theme, we specify the style of action mode like usual:在基本主题中,我们像往常一样指定动作模式的样式:

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

In our custom ActionModeStyle we do whatever styling we want, and also specify a theme attribute:在我们的自定义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 will also change the back button color, but we can easily override that one using actionModeCloseButtonStyle . textColorSecondary也会改变后退按钮的颜色,但我们可以使用actionModeCloseButtonStyle轻松覆盖那个actionModeCloseButtonStyle

First make your custom style首先制作您的自定义样式

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

Then just add it into your toolbar然后只需将其添加到您的工具栏中

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

如果您在活动 xml 中使用工具栏,您可以使用这样的东西

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

If you want the white overflow menu icon simply add android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" to your Toolbar layout code.如果您想要白色溢出菜单图标,只需将android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"到您的工具栏布局代码中。

If you want the dark overflow menu icon use android:theme="@style/Base.Widget.AppCompat.Light.PopupMenu"如果您想要深色溢出菜单图标,请使用android:theme="@style/Base.Widget.AppCompat.Light.PopupMenu"

So final code is something like:所以最终的代码是这样的:

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

Also, you should understand that it will change the color of the menu items also.此外,您应该明白它也会改变菜单项的颜色。

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

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

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