私はこのレイアウトを持っています:
<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
ます。
これは正しいです?これはベストプラクティスですか?これは可能ですか?
注: 私のコードは「static drawer_menu」で動作しています。これを改善したいと考えています。
私は待っています。
[編集]
私はそれを作る:
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﹕ FATAL EXCEPTION: main Process: com.example.sticdev30.newdrawer, PID: 15335 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sticdev30.newdrawer/com.example.sticdev30.newdrawer.MainActivity }: android.content.res.Resources$NotFoundException: 文字列リソース ID #0x1
KSMenuItem は、メニュー データを含む POJO です。kmi.id で、増分整数を通知しました...
待ってる