DrawerLayout是V4包下的一个控件,相信大家都是用过的,像我们熟悉的qq,酷狗之类的软件,总之这个DrawerLayout一定要搭配好。大家在使用AS创建项目的时候,有个Navigation Drawer Activity选项,当时还是小白的我,打开之后效果确实挺炫的,但是满满的一大坨代码,对于小白来说的却是很伤心,现在福利到了,我会一步步深入讲解DrawerLayout以及和ToolBar,Navigation,SnackBar搭配使用,让你一步步成为大佬。废话少说开始今天的正题,让你初步认识DrawerLayout!
-----------------------------------华丽的分割线--------------------------------
插一句题外话:大家建好一个空项目之后,为了方便全屏或者方便使用ToolBar,把主题更改一下:
<resources>
<!-- Base application theme. -->
<!-- parent继承NoActionbard的主题-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
-----------------------------------继续开车--------------------------------
在布局中把你需要使用抽屉的内容用DrawerLayout包裹起来,里面有侧边栏部分和内容部分,然后在侧边栏的布局中加入 android:layout_gravity="start",有start就有end,start是从左边侧滑的,end是从右边侧滑的。
ok下面来看效果:
左侧滑栏:
右侧滑栏:
---------------------------------华丽的分割线-----------------------------------------------------------------
好了,上图的代码撸起:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fly.dl01.MainActivity">
<!--内容部分的布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是内容部分" />
</LinearLayout>
<!--侧滑栏左边的布局-->
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#0f0"
android:orientation="vertical"
android:paddingTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item3" />
</LinearLayout>
<!--侧滑栏右边的布局-->
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#0f0"
android:orientation="vertical"
android:paddingTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="item3" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
----------------------------------------分割线-----------------------------------------------------------------
大家试着自己往侧边栏加点东西,自己去实现一下,哈哈!
----------------------------------------分割线-----------------------------------------------------------------
下一节带领大家继续讲解DrawerLayout和其他的组件配合使用。。。