效果
<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/draw_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局-->
<FrameLayout
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff99">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#D97D53"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</FrameLayout>
<!--侧边栏-->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
//从左边出现
android:layout_gravity="start"
android:fitsSystemWindows="true"
//头布局
app:headerLayout="@layout/navi_header"
//菜单布局
app:menu="@layout/navi_menu"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
我们给NavigationView添加了头布局和菜单布局,这两个布局文件都位于layout文件夹下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/ym4"
android:gravity="center"
>
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:src="@drawable/ym2"
/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="杨幂幂"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<group
android:checkableBehavior="single"
android:title="Home items">
<item
android:id="@+id/nav_blog"
android:icon="@drawable/ic_launcher"
android:title="博客地址" />
<item
android:id="@+id/nav_ver"
android:icon="@drawable/ic_launcher"
android:title="版本信息" />
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_launcher"
android:title="关于我" />
</group>
<item android:title="Sub items">
<menu>
<item
android:id="@+id/sub_exit"
android:icon="@drawable/ic_launcher"
android:title="退出应用" />
<item
android:id="@+id/sub_switch"
android:icon="@drawable/ic_launcher"
android:title="切换主题" />
</menu>
</item>
</menu>
最后我们在代码中控制NavigationView
public void initNavigationView(){
navigationView = (NavigationView) findViewById(R.id.navigationView);
drawerLayout = (DrawerLayout) findViewById(R.id.draw_layout);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home){
//打开抽屉侧滑菜单
drawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}