Ich habe dieses Layout:Dynamische Menüs in NavigationView
<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"/>
Also, auf der Suche, dass in NavigationView
haben wir das Attribut:
app:menu="@menu/drawer_menu"
Und ich habe eine dieser XML-ID Menü-Ordner .
Ich möchte Dinamicament Menüs machen, dh, in Code sollte ich die 'MenuItem' Objekte montieren und in NavigationView
setzen.
Das ist richtig? Das ist beste Praxis? Dies ist möglich?
Hinweis: Mein Code arbeitet mit static drawer_menu
, ich möchte es verbessern.
Ich warte.
[EDIT]
Ich mache es:
06-27 15: 26: 15,538 15335-15335/
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()); }
Aber dieser Fehler ist passiert? E/AndroidRuntime: FATALE AUSNAHME: Haupt Prozess: com.example.sticdev30.newdrawer, PID: 15335 java.lang.RuntimeException: kann Aktivität nicht starten ComponentInfo {com.example.sticdev30.newdrawer/com.example.sticdev30.newdrawer .MainActivity}: android.content.res.Resources $ NotFoundException: String Ressource ID # 0x1
KSMenuItem ist ein POJO mit meinen Menüdaten. In kmi.id informierte ich inkrementale ganze Zahlen ...
Ich waitning
Kasse meine Antwort hier: http: //stackoverflow.com/a/30706233/3544839 – Moinkhan
Bitte, siehe meine Bearbeitung. –