我想达到这样的效果:
当您单击文档/教程/支持时,菜单将转到次级菜单.它不是可扩展的列表视图,也不是父菜单标题下的子菜单项.
我尝试使用导航视图创建菜单,如下所示:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_event" android:title="Home" /> <item android:id="@+id/nav_profile" android:icon="@drawable/ic_dashboard" android:title="Perfil" /> </group> <item android:title="More Options"> <menu> <item android:icon="@drawable/ic_forum" android:title="Forum" /> <item android:icon="@drawable/ic_headset" android:title="Headset" /> </menu> </item> </menu>
解决方法
我相信,我存档了预期的效果:
背后的想法是清除()现有菜单并在每次用户点击菜单项时重新充气菜单,这应该导致子菜单. (并且回来 – 几乎一样)
作为根菜单(activity_main_drawer.xml),我使用了这个:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/action_item1" android:icon="@drawable/ic_menu_camera" android:title="Item 1" /> <item android:id="@+id/action_expand" android:icon="@drawable/ic_menu_gallery" android:title="Sub Menu to expand" /> </group> </menu>
这是一个子菜单(activity_submenu_drawer.xm):
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Go Back" android:icon="@drawable/ic_back" android:id="@+id/back_to_main"/> <item android:id="@+id/sub_item1" android:icon="@drawable/ic_menu_share" android:title="Subitem 1" /> <item android:id="@+id/sub_item2" android:icon="@drawable/ic_menu_send" android:title="Subitem 2" /> </menu>
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" /> </android.support.v4.widget.DrawerLayout>
这是MainActivity类(进行切换):
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { NavigationView navigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ..... navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.inflateMenu(R.menu.activity_main_drawer); navigationView.setNavigationItemSelectedListener(this); } .... @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.action_expand) { navigationView.getMenu().clear(); navigationView.inflateMenu(R.menu.activity_submenu_drawer); } else if (id == R.id.back_to_main) { navigationView.getMenu().clear(); navigationView.inflateMenu(R.menu.activity_main_drawer); } return true; } }
我已将我的测试项目上传到我的DropBox – feel free to check it out
我希望,这有帮助