在操作栏中更改溢出图标

112

在操作栏中更改溢出图标是否可能? 我的所有ActionBar项目都有蓝色图标,当溢出图标出现时,我也想更改它。


@ShirishHerwade:该链接已经失效。 - Al Lelopath
7个回答

258

你可以使用样式,但必须将其添加到主题声明中。

<resources>
    <!-- Base application theme. -->
    <style name="Your.Theme" parent="@android:style/Theme.Holo">
        <!-- Pointer to Overflow style ***MUST*** go here or it will not work -->
        <item name="android:actionOverflowButtonStyle">@style/OverFlow</item>
    </style>

    <!-- Styles -->
    <style name="OverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
        <item name="android:src">@drawable/ic_action_overflow</item>
    </style>

</resources>

你也可以动态地更改它,我在这里详细介绍了如何操作:

通过编程方式更改Android溢出菜单图标


6
请注意,这适用于“主题”,而不是“样式”。我因为没有意识到这一点而卡了一段时间,直到我意识到这一点。谢谢@aneal。 - Richard Le Mesurier
5
在运行Android 3.0(API 11)的平板电脑上,此代码将崩溃,因为它不允许定义样式“android:actionOverflowButtonStyle”(可能是平台错误)。因此,您必须在values-v11/styles.xml和values-v15/styles.xml中使用不同的样式。 - Mr-IDE
10
对于ActionBarSherlock的用户,父样式为:parent="Widget.Sherlock.ActionButton.Overflow" - Nick White
1
@BillMote 说实话,即使过了两年,我也不太明白。我可以看到当时我给adneal点了赞,所以他肯定帮助了我。我可能当时正在阅读其他帖子,这些帖子说了其他的事情,所以我才会觉得那条评论有意义。抱歉,我必须拒绝回答,并说也许当时收到+5是有意义的。再说,投票在这里并不重要。我将发布(作为答案)来自生产应用程序的最小代码,这可能有所帮助。 - Richard Le Mesurier
8
对于AppCompat(v21的Lollipop版本),parent="@style/Widget.AppCompat.ActionButton.Overflow" 对我来说似乎有效。 - fattire
显示剩余3条评论

16

如果您无法使其工作,请尝试去掉“android:”命名空间。

<item name="actionOverflowButtonStyle">@style/OverFlow</item>

谢谢兄弟,这真的帮了我很多。有人可以解释一下吗? - DaMachk
4
如果你正在使用AppCompat,则不要包含“android:”命名空间。 - matiash

5
  1. Change your custom overflow icon of Actionbar in styles.xml.

     <resources>
        <!-- Base application theme. -->
        <style name=“MyTheme" parent="@android:style/Theme.Holo">
           <!-- Pointer to Overflow style DONT forget! -->
           <item name="android:actionOverflowButtonStyle">@style/MyOverFlow</item>
        </style>
    
        <!-- Styles -->
        <style name="MyOverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
            <item name="android:src">@drawable/ic_your_action_overflow</item>
        </style>
     </resources>
    
  2. Put custom theme "MyTheme" in application tag of AndroidManifest.xml

    <application
        android:name="com.example.android.MainApp"
        android:theme="@style/AppTheme">
    </application>
    

玩得开心。@.@


5
无论我在styles.xml中尝试做什么,都没有起作用。最终,如果你拥有AppCompat 23+,这是实际可行的最简单方法。
toolbar.setOverflowIcon(drawable);

我发现的最准确的方法! - Shubhamhackz

1

@adneal's answer 是正确的(因此这只是一个社区维基说明来扩展它)。

其中一条评论建议可能存在误解如何做到这一点,因此我的答案只是另一个关于如何覆盖溢出图标图像的示例。

下面的代码基于ADT示例新Android项目的模板样式。该代码来自res/values文件夹中的styles.xml文件。


<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionOverflowButtonStyle">@style/OverflowMenuButton</item>
</style>

<style name="OverflowMenuButton" parent="@android:style/Widget.Holo.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_menu_moreoverflow</item>
</style>

上述代码的作用是将默认的溢出图标替换为您命名为ic_menu_moreoverflow的任何可绘制对象。如果您想快速测试它,请使用您的应用程序启动器图标文件名,这将使它的工作方式变得明显。
在尝试理解ActionBar样式时可以使用的有用资源是: 在那里,您可以定义特定的颜色来使用您的溢出图标,并且ZIP文件将为您包括正确的styles.xml文件。

1

在长时间的搜索之后,我得到了以下应遵循的指导:

  1. 检查宣言文件中的主题。
  2. 然后转到res->values->themes。
  3. 在此文件中找到主题名称。
  4. 在theme.xml中的主题中添加项目:

    <item name="android:actionBarWidgetTheme">@style/widgetStyle</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
    
  5. 在同一文件中的样式中添加图标,我的意思是theme.xml:

    <style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.ActionButton.Overflow">
    <item name="android:src">@drawable/icon_menu</item>
    </style>
    
  6. 查看要更改的自定义图标

它对我有效


1
对于溢出图标颜色的更改,您只需添加:

toolbar.setOverflowicon(getDrawable(R.id.whiteIcon));

或者

toolbar.setOverflowicon(getDrawable(R.id.redIcon));

它将更改图标,选择您想要的任何颜色并设置为Overflowicon。

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