ho questo Disposizione:menu dinamici 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"/>
Così, guardando che in NavigationView
abbiamo l'attributo:
app:menu="@menu/drawer_menu"
e ho una cartella di questo menu id XML .
Voglio fare i menu dinamicament, cioè, nel codice dovrei montare gli oggetti "MenuItem" e impostare su NavigationView
.
Questo è corretto? Questa è la soluzione migliore? Questo è possibile?
Nota: il mio codice funziona con static drawer_menu
, voglio migliorarlo.
Sto aspettando.
[EDIT]
faccio:
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());
}
Ma questo errore è accaduto:
06-27 15: 26: 15,538 15.335-15.335 /? E/AndroidRuntime: FATAL EXCEPTION: main Processo: com.example.sticdev30.newdrawer, PID: 15335 java.lang.RuntimeException: impossibile avviare l'attività ComponentInfo {com.example.sticdev30.newdrawer/com.example.sticdev30.newdrawer .MainActivity}: android.content.res.Resources $ NotFoundException: stringa di risorsa ID # 0x1
KSMenuItem è un POJO con i miei dati del menu. In kmi.id Ho informato interi incrementali ...
sto waitning
checkout la mia risposta qui: http: //stackoverflow.com/a/30706233/3544839 – Moinkhan
Per favore, vedere la mia modifica. –