在ActionBar上更改溢出按钮的颜色

67

是否可以更改操作栏中溢出按钮(3个垂直点)的颜色?如果可以,我们该如何做到呢?我没有找到任何关于溢出按钮的样式。

谢谢


6
可以的,将<item name="colorControlNormal">#fff</item>添加到您的样式中。 - meyasir
15个回答

72
你可以使用以下样式声明来更改所使用的图像。
<style name="MyCustomTheme" parent="style/Theme.Holo">
    <item name="android:actionOverflowButtonStyle">@style/MyCustomTheme.OverFlow</item>
</style>

<style name="MyCustomTheme.OverFlow">
    <item name="android:src">@drawable/my_overflow_image</item>
</style>

如果您正在使用ActionBarSherlock,以下是如何执行的方法

<style name="MyCustomTheme" parent="style/Theme.Sherlock">
    <item name="android:actionOverflowButtonStyle">@style/MyCustomTheme.OverFlow</item>
    <item name="actionOverflowButtonStyle">@style/MyCustomTheme.OverFlow</item>
</style>

<style name="MyCustomTheme.OverFlow">
    <item name="android:src">@drawable/my_overflow_image</item>
</style>

5
由于某种原因,在Galaxy Nexus(JellyBean)上使用目标sdk 15时,这对我不起作用。三个点的菜单按钮仍然保持不变。 - IgorGanapolsky
13
为保留默认的溢出按钮设置(例如高度/宽度、触摸覆盖图像等),并仅更改图标图像,需将父级添加到自定义溢出样式中:parent="@android:style/Widget.ActionButton.Overflow" - jokeefe
4
那么如果不添加全新的图像,就没有办法改变“...”图标的颜色了吗? - VinceFior
1
它从textColorPrimary中选择颜色,因此与其编写多行代码,这一行<item name="android:textColorPrimary">@android:color/white</item>就可以解决你的问题。 只有一个小问题是,这种颜色也会应用于你的溢出菜单的文本颜色,当然还有很多其他地方。 - Neo
2
如果您不想更改图像,请查看https://dev59.com/KFsV5IYBdhLWcg3wrAdJ - bitrock
显示剩余4条评论

41

这些答案中有一些过于复杂,而有些又只能得到有限的结果。答案要简单得多。您可以随时将其设置为任何颜色。

toolbar.getOverflowIcon().setColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP);

4
太棒了!经过几个小时的搜索,它终于起作用了。不要使用colorInt,而是使用ContextCompat.getColor(this, R.color.gray) - CoolMind
2
这是正确的方法。它还可以改变返回按钮的颜色。 - olfek
4
我以以下方式使用了 setTint,因为 setColorFilter 已经被弃用:.setTint(ContextCompat.getColor(this, R.color.red)) - AlvaroSantisteban
@if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) - YoussefDir
1
Toolbar toolbar = findViewById(R.id.toolbar); You can use a Toolbar in xml for the ActionBar by using the method setSupportActionBar(toolbar); - John Glen
显示剩余4条评论

20

XGouchet的回答非常好,但是我想补充一点,如果你继承一个现有的样式,你会得到标准的填充、选中状态等。

<style name="MyCustomTheme.OverFlow" parent="Widget.Sherlock.ActionButton.Overflow">
    <item name="android:src">@drawable/title_moreicon</item>
</style>

1
parent="android:Widget.ActionButton.Overflow" - Alexander Sidikov Pfeif
对于我来说:parent="@android:style/Widget.Holo.Light.ActionButton.Overflow" - jpprade

11

其他编程选项:

        Drawable drawable = toolbar.getOverflowIcon();
        if(drawable != null) {
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.thecolor));
            toolbar.setOverflowIcon(drawable);
        }

希望这能帮到你!


10

3
如果你想改变小圆点的颜色,而不想麻烦太多,我认为这是最实用的方法! - Vince V.

3

放置图像并不是一个好主意,因为如果您只需更改颜色即可更改它,则无需使用额外的图像。 菜单点从textColorPrimary中选择颜色,因此,与其编写多行,这个单行代码 <item name="android:textColorPrimary">@android:color/white</item> 将解决您的问题。 唯一的小问题是,这个颜色也会应用于您的溢出菜单的文本颜色,当然还有许多其他地方 :)


这是 android:textColorSecondary。 - shmoula

2
如果您正在使用最新的工具栏,您需要将该语句放置在styles.xml中,如下面的代码所示:
<style name="AppToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/black</item>
</style>

2

要将溢出图标颜色更改为您选择的颜色.....使用它们

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     <item name="actionOverflowButtonStyle">@style/actionButtonOverflow</item>
</style>
 <style name="actionButtonOverflow"   parent="Widget.AppCompat.Light.ActionButton.Overflow">
    <item name="android:tint">@color/primary_dark</item>
</style>

1
<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light">
  <item name="actionOverflowButtonStyle">@style/OverflowMenuButtonStyle</item>
</style>
<style name="OverflowMenuButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
  <item name="android:src">@drawable/quantum_ic_more_vert_white_24</item>
</style>

请注意,这与XGouchet的答案不同,因为它删除了android命名空间。 XGouchet的答案仅适用于棒棒糖及更高版本的设备,而我的答案适用于所有API 7+设备。
参考:Custom AppCompat Theme not changing Overflow icon on older devices

1

只需在style.xml文件中选择的样式中添加此行

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

它可以正常工作。 如果您使用

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

那么,您的其他项目,如抽屉导航菜单图标颜色和单选按钮,也会受到影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接