更改操作栏溢出图标

4
我已经在values-v11文件夹中应用了以下代码。
styles.xml
 <style name="actionBarTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
 </style>

 <style name="MyActionButtonOverflow" parent="@android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/action_item_btn</item>
 </style>

但它没有改变溢出图标,这个主题中的所有其他项目都运行良好,但不包括这一个,我有什么遗漏吗?

你应该把样式放在相应的res/values文件夹中。 - Raghunandan
但我想将这些风格应用到4.0版本以上。 - Kanika
那么你的样式文件在哪个values文件夹中? - Raghunandan
你试过了吗?它有效吗? - Raghunandan
3个回答

10
 i want to apply these styles above 4.0 version 

但您可以在values-v11中找到样式。

另请注意。

parent="android:Theme.Holo.Light" // in your code

parent="@android:style/Theme.Holo" // mine

应该放在values-v14中。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
    </style>
    <style name="MyActionButtonOverflow" parent="@android:style/Widget.Holo.Light.ActionButton.Overflow">
       <item name="android:src">@drawable/ic_launcher</item>    
    </style>
</resources>
Snap

请忽略UI的外观,这只是测试。但是您会发现启动器图标作为溢出菜单图标。

输入图片描述

编辑:

这是我的测试样本

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
             <item name="android:actionBarStyle">@style/MyActionBar</item>
              <item name="android:actionBarDivider">@color/red</item>
              <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
         <item name="android:background">#FF9D21</item>

         <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style> 
    <style name="MyActionBarTitleText"
           parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/red</item>
        <item name="android:textStyle">bold</item>
    </style>
      <style name="MyActionButtonOverflow" parent="@android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/launcher</item>    
    </style>
    <style name="ActionButton" parent="@android:style/Widget.Holo.Light.ActionButton">
</style>
</resources>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>
Snap

好的,我明白了。非常感谢。另外,我想在操作栏末尾添加一个分隔符。我正在使用<item name=android:actionbardivider>,但它仍然无法工作。 - Kanika
@Kanika,那是另一个问题。对于所提出的问题,我已经回答了。 - Raghunandan
我不知道为什么在我的电脑上没有运行?我已经按照你说的做了。 - Kanika
@Kanika 我只能发布可行的和可能可行的内容。我不知道你在做什么。 - Raghunandan
@Raghunandan 需要询问我们需要在values和values-v11样式文件中放置什么... 我还遇到了IllegalException并得到了建议使用Theme.Appcompat。 - AndroidHacker
1
@AndroidHacker,这是因为您需要使用AppCompat库,并且应该使用AppCompat主题。有关values-11,请参阅文档,所有内容都在那里。 - Raghunandan

1
我认为你应该做以下操作:
<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>

@Kanika,请在values-v14/styles.xml中查找样式,然后尝试。 - Raghunandan

0

你可以在Java中以编程方式设置它,如下所示:

toolbar.setOverflowIcon(getResources().getDrawable(R.drawable.your_icon, null));

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