Android开发之DrawerLayout的简单使用

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和其他的组件配合使用。。。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值