Navigation drawer导航抽屉,被设计用于应用导航,提供了一种通用的导航方式。他是android5.0后引入的,其中qq的主界面的侧滑菜单也是由它衍生。以下使我我自己设计的一个NavigationView
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Content -->
<FrameLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="@layout/main"
app:menu="@menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
其中
FrameLayout是主界面content,第二个chird是侧滑界面注意其中NavigationView的两个自定义属性app:headerLayout接收一个layout,可选项。app:menu接收一个menu,这个是必须选择的一项。
NavigationView提供的OnNavigationItemSelectedListener的方法为我们选中的item添加点击事件。通过这个方法我们可以为我们的item提供我们需要执行的操作
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_scanner) {
Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
} else if (id == R.id.nav_add) {
Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
} else if (id == R.id.nav_about) {
Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
} else if (id == R.id.nav_search) {
Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

本文介绍了Android中用于应用导航的Navigation Drawer,特别是重点讨论了NavigationView的使用和设计。内容包括NavigationView的基本概念,以及如何自定义一个符合设计需求的NavigationView,通过实例展示了其在QQ主界面侧滑菜单中的应用。
247

被折叠的 条评论
为什么被折叠?



