我为我的应用程序使用了可以在android studio中添加的导航抽屉,并且向Nav-drawer添加了一些菜单项(请参见代码块).如果我按第一个项目(nav_home)或第二个,则导航抽屉会高亮显示当前按下的项目.
如果我按下项目“ nav_information”按钮或其他项目,则新的(单击的)片段将打开并且看不到突出显示的内容(主页或第二个仍然突出显示的内容)
不久,只有前几项在所选项目上显示突出显示.
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black"
android:title="Startseite" />
<item android:id="@+id/nav_greetings"
android:title="Grußwort"/>
</group>
<group android:checkableBehavior="single">
<item android:title="Neuigkeiten">
<menu >
<item
android:id="@+id/nav_information"
android:icon="@drawable/ic_info_black"
android:title="@string/title_activity_aktuelles" />
</menu>
</item>
</group>
....
这就是我处理片段打开方式的方式.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment myFragment = null;
if (id == R.id.nav_home) {
drawer.setSelected(true);
myFragment = new MainScreen();
}else if (id == R.id.nav_greetings){
myFragment = new Greetings();
} else if (id == R.id.nav_information) {
myFragment = new Aktuelles();
} else if (id == R.id.nav_directions) {
myFragment = new Anfahrt();
} else if (id == R.id.nav_basilika) {
myFragment = new Rundabout();
} else if (id == R.id.nav_download) {
myFragment = new Downloads();
} else if (id == R.id.nav_history) {
myFragment = new History();
}else if(id == R.id.nav_hauptwallfahrt){
myFragment = new Hauptwallfahrt();
}else if (id == R.id.nav_themenwallfahrt){
myFragment = new Themenwallfahrt();
}else if (id == R.id.nav_pilgergruppen){
myFragment = new Pilgergruppen();
}else if (id ==R.id.nav_wallfahrtgodi){
myFragment = new Wallfahrtsgottesdienste();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
// Replace the requestet Fragment
ft.replace(R.id.container, myFragment) ;
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
有办法“启用”突出显示吗?
提前致谢!
解决方法:
您只需要更改标签的顺序
我尝试了最后一组,但可以,请检查
<item android:title="Neuigkeiten">
<menu >
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_information"
android:icon="@drawable/ic_info_black"
android:title="@string/title_activity_aktuelles" />
</group>
</menu>