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

问题描述 投票:20回答:9

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

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

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

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

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

android overflow toolbar android-appcompat actionmode
9个回答
60
投票

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

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

0
投票
<style name="ToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:tint">@color/colorAccent</item>

使用您的颜色创建上述theme.set色调,并将此主题添加到工具栏。

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

14
投票

这可以通过设置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>

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

在res-> styles.xml上添加此代码

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

那么您的XML文件中的“工具栏颜色”样式就像心爱的那样

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

2
投票

要正确更改工具栏溢出菜单图标的颜色,请将工具栏的主题设置为AppCompat dark 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>

2
投票
<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>

1
投票

首先制作自定义样式

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

1
投票

如果您使用活动xml中的工具栏,则可以使用类似这样的内容

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

0
投票

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

如果你想要黑暗溢出菜单图标使用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"
        />

此外,您应该了解它也会改变菜单项的颜色。

© www.soinside.com 2019 - 2024. All rights reserved.