gpt4 book ai didi

android - 动态添加/删除项目菜单menuItem

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:34 24 4
gpt4 key购买 nike

我有一个问题,我想以编程方式在工具栏( Material 设计)中添加项目。

这是我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>

我知道更改文本、颜色、背景和@override 监听器

toolbar.setBackgroundColor
toolbar.setTextColor
toolbar.setNavigationIcon
toolbar.setText

我不知道如何添加类别为 "android:orderInCategory="300"

的菜单项

谢谢。

注意:我有所有 fragment ,没有 1 个 Activity

Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx ..

最佳答案

试试这个工具栏有返回菜单的选项 getMenu()

private static final String placeholder1="Something";
private static final int FIRST_ID=Menu.FIRST;
private static final int SECOND_ID=Menu.FIRST+1;


//To add an item
toolbar.getMenu().add(Menu.NONE,FIRST_ID,Menu.NONE,R.string.placeholder1);
toolbar.getMenu().add(Menu.NONE,SECOND_ID,Menu.NONE,R.string.placeholder2);

//and to remove a element just remove it with id
toolbar.getMenu().removeItem(FIRST_ID);

关于android - 动态添加/删除项目菜单menuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31826034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com