1.首先定义主布局
<?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/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.dejun.androidcourse.activity.ThirdActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false"> <com.dejun.commonsdk.wight.CustomSearchView android:id="@+id/csv" android:layout_width="match_parent" android:layout_height="wrap_content" app:search_backgound="#ff00" app:search_hint="搜索内容" app:search_left_icon="@drawable/arrow_left_back" app:search_right_text="完成"></com.dejun.commonsdk.wight.CustomSearchView> <com.dejun.commonsdk.wight.BottomMenuView android:id="@+id/bmv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:fifth_drawable="@drawable/tab_better_pressed" app:fifth_text="你好" app:menu_center_visible="true" app:menu_text_color="#000" app:menu_text_size="12sp" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/csv" android:layout_above="@id/bmv"> </FrameLayout> </RelativeLayout> <android.support.design.widget.NavigationView android:id="@+id/navigationview" android:layout_width="320dp" android:layout_height="match_parent" android:layout_gravity="start"//定义抽屉式布局的左侧布局 app:headerLayout="@layout/drawer_header_layout"/左侧放置头像的布局 app:menu="@menu/drawer_menu"//左侧放置菜单的布局 app:itemIconTint="#ff00"//定义菜单图片颜色 /> </android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="192dp" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/left_head_background" android:scaleType="centerCrop"/> <ImageView android:layout_width="96dp" android:layout_height="96dp" android:padding="8dp" android:src="@drawable/default_avatar"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:padding="16dp" android:textColor="#000" android:textSize="17sp" android:text="魏群" /> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <group android:checkableBehavior="single"> <item android:id="@+id/navigation_item_one" android:icon="@drawable/drawer_default_icon" android:title="文件" /> <item android:id="@+id/navigation_item_two" android:icon="@drawable/drawer_default_icon" android:title="目录" /> <item android:id="@+id/navigation_item_three" android:icon="@drawable/drawer_default_icon" android:title="佳佳" /> <item android:id="@+id/navigation_item_four" android:icon="@drawable/drawer_default_icon" android:title="闹钟" /> <item android:id="@+id/navigation_item_five" android:icon="@drawable/drawer_default_icon" android:title="设置" /> <!--<item--> <!--android:id="@+id/navigation_subheader_four"--> <!--android:icon="@drawable/drawer_default_icon"--> <!--android:title="子条目">--> <!--<menu>--> <!--<item--> <!--android:id="@+id/navigation_sub_item_eight"--> <!--android:icon="@drawable/drawer_default_icon"--> <!--android:title="佳佳" />--> <!--<item--> <!--android:id="@+id/navigation_sub_item_nine"--> <!--android:icon="@drawable/drawer_default_icon"--> <!--android:title="闹钟" />--> <!--</menu>--> <!--</item>--> </group> </menu> 3.使用
drawerLayout=findViewById(R.id.drawerlayout); navigationview=findViewById(R.id.navigationview); headerView = (FrameLayout) navigationview.getHeaderView(0); navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_item_one: break; case R.id.navigation_item_two: break; case R.id.navigation_item_three: break; case R.id.navigation_item_four: break; case R.id.navigation_item_five: break; } return false; } }); 4.关闭侧滑菜单
drawerLayout.openDrawer(Gravity.LEFT);