侧滑菜单的实现,本博客知道的就两种,slidingMenu和DrawLayout两种,这一篇为大家介绍一下android自带的侧滑菜单drawLayout,drawLayout使用起来非常方便,主要是xml布局中实现,下面是布局
drawLayout就是把要侧滑的布局全部放在
android.support.v4.widget.DrawerLayout的布局内, 固定在中间的使用属性android:gravity="center" 从左边侧滑的布局需要设置的属性是:android:layout_gravity="left"如果这些属性没有提示,不要认为错误,手动打出这些属性从右边侧滑的布局需要设置的属性是:android:layout_gravity="right"
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dlMenu" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp_main" > </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/left_sliding" android:layout_gravity="left" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl_left" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout> <LinearLayout android:id="@+id/right_sliding" android:layout_gravity="right" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl_right" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout> </android.support.v4.widget.DrawerLayout> 在activity中的调用控件DrawLayout drawerLayout = (DrawerLayout) findViewById(R.id.dlMenu); 最后为大家介绍常用的方法 drawerLayout.openDrawer(ll_left);//打开左侧滑drawerLayout.openDrawer(ll_right);//打开右侧滑drawerLayout.closeDrawer(ll_left);//关闭左侧滑drawerLayout.closeDrawer(ll_right);//关闭右侧滑drawerLayout.closeDrawers();//关闭所有侧滑
判断是否打开drawerLayout.isDrawerOpen(ll_right);//判断右边是否打开drawerLayout.isDrawerOpen(ll_left);//判断左边是否打开 //监听drawerLayout.addDrawerListener(); 这里面可以监听到侧滑的开始和结束还有侧滑时的状态 介绍结束