DrawerLayout是一种Material Design风格的侧滑菜单,v4包里提供了该控件,可以轻松实现侧滑效果,通常与Toolbar和Navigation控件搭配。
实现效果:
实现步骤:
1.在XML文件中,把最外层布局改成DrawerLayout
*******
*******
*******
*******
*******
*******
2.编辑弹出菜单的布局,我这里设置了一个线性布局LinearLayout,里面放了一个TextView
android:id= “@+id/main_ll” android:width= “match_parent” android:height= “match_parent” android: 0background= “@color/primaryColor” android:oritention= “vertical”> android:id=”@+id/main_ll_tv” android:width=”wrap_content” android:height=”wrap_content” android:text=”Fantasychong丶” android:textsize=”16sp” />
运行效果:
这时候点击右边的空白区域并不能关闭菜单,而且没有阴影影响观感,在xml文件中加一层FrameLayout即可解决这个问题。
android:width=”match_parent” android:height=”match_parent” >
运行效果:
这时发现侧滑菜单没有覆盖顶部的导航栏,影响观感,于是决定去掉系统默认的导航栏,而使用ToolBar
1.打开style.xml文件,先将系统主题AppTheme改为NoActionBar
2.在xml文件中刚才添加的FrameLayout中设置Toolbar
android:width= “match_parent” android:height= “match_parent”> android:id=”main_toolbar” android:width= “match_parent” android:height= “match_parent” android:theme= “@style/ThemeOverlay.AppCompat.Dark.ActionBar” app:popupTheme=”@style/ThemeOverlay.AppCompat.Light”>
3.打开目标Activity,初始化ToolBar
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
toolbar=(Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
}
运行结果:
Demo下载
http://download.csdn.net/download/u014078990/9960507
点击打开链接