我有这个布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/nav_header"/>
所以,看看NavigationView
我们有这个属性:
app:menu="@menu/drawer_menu"
我有这个 XML id 菜单文件夹。
我想制作动态菜单,即在代码中我应该挂载“MenuItem”对象并设置为NavigationView
.
它是否正确?这是最好的做法吗?这可能吗?
注意:我的代码正在使用“静态抽屉菜单”,我想改进它。
我在等待。
[编辑]
我做到了:
Menu menu = nvDrawer.getMenu();
for (KSMenuItem kmi : menus.values()) {
if (menu.size() == 0) {
menu.add(kmi.getId());
}
if (menu.getItem(kmi.getId()) == null) {
menu.add(kmi.getId());
}
MenuItem mi = menu.getItem(kmi.getId());
mi.setIcon(kmi.getIcon());
mi.setTitle(kmi.getTittle());
}
但是发生了这个错误:
06-27 15:26:15.538 15335-15335/? E/AndroidRuntime:致命异常:主进程:com.example.sticdev30.newdrawer,PID:15335 java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.sticdev30.newdrawer/com.example.sticdev30.newdrawer.MainActivity }: android.content.res.Resources$NotFoundException: 字符串资源 ID #0x1
KSMenuItem 是带有我的菜单数据的 POJO。在 kmi.id 中,我通知了增量整数...
我在等