(布局)
<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" tools:context="zhangyanran201800809.bwie.com.day07.activity.MainActivity"> <!--主页面--> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorAccent"> <ImageView android:id="@+id/head_portrait" android:layout_width="50dp" android:layout_height="match_parent" android:src="@mipmap/ic_launcher"/> </LinearLayout> </RelativeLayout> <!--侧拉页面--> <RelativeLayout android:background="#0f0" android:layout_width="320dp" android:layout_gravity="start" android:layout_height="match_parent"> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
(图片的点击打开侧滑)
head_portrait.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 判断侧滑菜单是否是在左边打开 if (drawerLayout.isDrawerOpen(Gravity.START)) { drawerLayout.closeDrawer(Gravity.START); } else { drawerLayout.openDrawer(Gravity.START); } }
*****写完点击事件即可大开侧滑,但是加上一下代码即可实现QQ样式的点击侧滑******
head_portrait.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 判断侧滑菜单是否是在左边打开 if (drawerLayout.isDrawerOpen(Gravity.START)) { drawerLayout.closeDrawer(Gravity.START); } else { drawerLayout.openDrawer(Gravity.START); } drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { //滑动过程中不断回调 slideOffset:0~1 View content = drawerLayout.getChildAt(0); View menu = drawerView; float scale = 1 - slideOffset;//1~0 content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width } @Override public void onDrawerOpened(@NonNull View drawerView) { } @Override public void onDrawerClosed(@NonNull View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } }); } });