gpt4 book ai didi

android - 以编程方式创建带有副标题的 Android MenuItem

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:26 26 4
gpt4 key购买 nike

我需要一个带有以编程方式生成的项目的 DrawerMenu,我可以添加 menuItems,但只有一个标题和一个图标,我希望它们也有一个副标题(如图所示)

enter image description here

我通过它的 ID 获取抽屉,然后是它的菜单,然后我像这样添加 MenuItems(来 self 的 MainActivity 的代码 fragment ):

int id =0;
String MenuTitle = "Title of the item in menu";
NavigationView mDrawerList = (NavigationView)findViewByID(R.id.nav_view);
Menu menu = mDrawerList.getMenu();
MenuItem menuItem;
menuItem = menu.add (Menu.NONE, id, Menu.NONE, title);
menuItem.setIcon(R.drawable.ic_action_home);
menuItem.setCheckable(true);

最佳答案

目前 NavigationView 不允许在其菜单中添加子标题。

但如果你想实现这一点,我会建议你将 ListView 嵌套在 NavigationView 中。 这样你就可以自定义你的 ListView电池根据您的需要。

<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/nav_header" />

<ListView
android:id="@+id/lst_menu_items"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.design.widget.NavigationView>

注意:如果您同时使用 ListView 和 Menu 文件,请不要忘记从 NavigationView 中删除 app:menu。它会与菜单项上的 ListView 重叠。

关于android - 以编程方式创建带有副标题的 Android MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35578034/

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