赞
踩
实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)
- <android.support.v4.widget.DrawerLayout
- android:id="@+id/drawer_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <!-- 内容区 -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="20dp"
- android:text="内容区"
- android:textSize="20sp"/>
-
- <Button
- android:id="@+id/btn_open_left"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开左边"/>
-
- <Button
- android:id="@+id/btn_open_right"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开右边"/>
-
- </LinearLayout>
-
- <!-- 左边菜单 -->
- <android.support.design.widget.NavigationView
- android:id="@+id/navigation_view"
- android:layout_width="260dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- app:headerLayout="@layout/drawer_header"
- app:menu="@menu/menu_drawer_left"/>
-
- <!-- 右边菜单 -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="end"
- android:background="@color/black"
- android:gravity="center"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="20dp"
- android:text="右边菜单"
- android:textColor="@color/white"
- android:textSize="20sp"
- android:textStyle="bold"/>
-
- <Button
- android:id="@+id/btn_close_right"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="关闭"/>
-
- </LinearLayout>
-
- </android.support.v4.widget.DrawerLayout>
android:layout_gravity="end"
标识从左边还是右边滑出GravityCompat.START方式更好
- //监听
- mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
- @Override
- public void onDrawerSlide(@NonNull View view, float v) {
- Log.i("---", "滑动中");
- }
-
- @Override
- public void onDrawerOpened(@NonNull View view) {
- Log.i("---", "打开");
- }
-
- @Override
- public void onDrawerClosed(@NonNull View view) {
- Log.i("---", "关闭");
- }
-
- @Override
- public void onDrawerStateChanged(int i) {
- Log.i("---", "状态改变");
- }
- });
- mDrawerToggle = new ActionBarDrawerToggle(
- this, /* host Activity */
- mDrawerLayout, /* DrawerLayout object */
- R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
- R.string.drawer_open, /* "open drawer" description for accessibility */
- R.string.drawer_close /* "close drawer" description for accessibility */
- ) {
- public void onDrawerClosed(View view) {
- getActionBar().setTitle(mTitle);
- invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
- }
- public void onDrawerOpened(View drawerView) {
- getActionBar().setTitle(mDrawerTitle);
- invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
- }
- };
- mDrawerLayout.setDrawerListener(mDrawerToggle);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawerLayout, R.string.home_drawer_open, R.string.home_drawer_close) {
- @Override
- public void onDrawerSlide(View drawerView, float slideOffset) {
- //可以重新侧滑方法,该方法实现侧滑动画,整个布局移动效果
- //获取mDrawerLayout中的第一个子布局,也就是布局中的RelativeLayout
- //获取抽屉的view
- View mContent = drawerLayout.getChildAt(0);
- float scale = 1 - slideOffset;
- float endScale = 0.8f + scale * 0.2f;
- float startScale = 1 - 0.3f * scale;
-
- //设置左边菜单滑动后的占据屏幕大小
- drawerView.setScaleX(startScale);
- drawerView.setScaleY(startScale);
- //设置菜单透明度
- drawerView.setAlpha(0.6f + 0.4f * (1 - scale));
-
- //设置内容界面水平和垂直方向偏转量
- //在滑动时内容界面的宽度为 屏幕宽度减去菜单界面所占宽度
- mContent.setTranslationX(drawerView.getMeasuredWidth() * (1 - scale));
- //设置内容界面操作无效(比如有button就会点击无效)
- mContent.invalidate();
- //设置右边菜单滑动后的占据屏幕大小
- mContent.setScaleX(endScale);
- mContent.setScaleY(endScale);
- }
- };
-
- toggle.syncState();
- drawerLayout.addDrawerListener(toggle);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。