【Android】DrawerLayout抽屉布局的写法

120 阅读1分钟

抽屉布局就类似那种侧拉出来的界面。

一个页面想要实现抽屉布局很简单!

这个XML文件包含抽屉布局的代码。

什么意思呢?

就是这个XML布局=主布局+抽屉布局!

懂了吧。

所以两个界面的布局都要写在同一个XML文件中。

那XML文件怎么分辨我们这个是主布局还是抽屉布局呢?

好问题,当我们在抽屉布局的最顶层布局Layout中加入下面这一行

android:layout_gravity="end"

XML就可以分辨出来这里面的是抽屉布局了。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     主布局内容...(略)

     ---以下是抽屉布局代码---   
     <RelativeLayout
                android:layout_width="wrap_content"
                android:minWidth="200dp"
                android:background="@color/gray"
                android:layout_height="match_parent"
                android:clickable="true"
                android:layout_gravity="end"
                android:focusable="true">

                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="1dp"
                    android:layout_marginEnd="1dp"
                    android:layout_marginStart="1dp"
                    android:layout_marginTop="1dp"
                    android:background="@color/title_bar_color"
                    android:padding="10dp"
                    android:text="选择查询的时间"
                    android:textColor="@color/white" />

                <android.support.v7.widget.LinearLayoutCompat
                    android:id="@+id/bottom"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:layout_alignParentBottom="true">

                    <android.support.constraint.ConstraintLayout
                        android:layout_width="0dp"
                        android:layout_height="46dp"
                        android:background="@drawable/round_corners"
                        android:layout_margin="1dp"
                        android:layout_weight="1">
        
                        <CheckBox
                            android:id="@+id/selectAll"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="@string/select_all"
                            android:textColor="@color/gray"
                            android:textSize="12sp"
                            android:layout_weight="1"/>
        
                    </android.support.constraint.ConstraintLayout>

                    <TextView
                        android:id="@+id/cancel"
                        android:layout_width="0dp"
                        android:layout_height="46dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="@drawable/round_corners"
                        android:gravity="center"
                        android:padding="10dp"
                        android:text="@string/cancel" />

                    <TextView
                        android:id="@+id/confirm"
                        android:layout_width="0dp"
                        android:layout_height="46dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="@drawable/round_corners"
                        android:gravity="center"
                        android:padding="10dp"
                        android:text="@string/confirm" />

                </android.support.v7.widget.LinearLayoutCompat>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_above="@id/bottom"
                    android:layout_below="@id/title" />

     </RelativeLayout>
    ---抽屉布局代码end---
</android.support.v4.widget.DrawerLayout>

就是在XML文件中 插入这样一坨代码。

布局完成!

来看效果图。