【问题标题】:How to change menu icon如何更改菜单图标
【发布时间】:2014-10-31 01:13:29
【问题描述】:

我在主要活动中声明了以下菜单:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

 //... populate with menu.add()

}

菜单图标看起来像 3 个点,它的位置在右侧的操作栏上。在我的应用程序中,我使用片段,我想根据它前面的片段更改菜单图标(我按下打开菜单项的图标)。

我怎样才能以编程方式做到这一点?

另外,在我的一些片段上,我不希望菜单出现。为此,我在这些片段中使用:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
}

我尝试找到一种使用菜单变量来更改菜单图标的方法。但是我没有找到办法。

那么....你能告诉我如何以编程方式更改菜单图标吗?

谢谢,

【问题讨论】:

  • 你是说导航抽屉吗?
  • noup。那个我改变它:新的ActionBarDrawerToggle。
  • 在默认应用程序中:您按下菜单图标(我想用其他图标更改的 3 个点),它会显示一个名为“设置”的项目
  • 你的代码中有这个吗?? ` mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) `

标签: android android-fragments menu


【解决方案1】:

如果我理解正确,您的意思是overflow 图标。我就是这样做的:

<!--inside your styles.xml-->
<style name="AppBaseTheme" parent="@android:style/Theme.Holo">
<item name="android:actionOverflowButtonStyle">@style/overflow</item>
</style>

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

更新:

以编程方式更改它:Changing the Android Overflow menu icon programmatically

【讨论】:

    【解决方案2】:

    看看这个问题:

    How to change the Option menu icon in the Action Bar?

    试试这个:

    <style name="YourTheme" parent="@android:style/Theme.Holo.Light">
         <item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
    </style>
    
    <style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
         <item name="android:src">@android:drawable/ic_menu_moreoverflow_holo_dark</item>
         <item name="android:background">?android:attr/actionBarItemBackground</item>
         <item name="android:contentDescription">@string/action_menu_overflow_description</item>
    </style>
    

    更新:

    要以编程方式更改它,请参阅以下问题: Changing the Android Overflow menu icon programmatically

    希望这会对你有所帮助。

    【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-03-17
    • 2023-04-02
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多