android側滑菜单-DrawerLayout的基本使用

时间:2023-03-08 23:12:06
android側滑菜单-DrawerLayout的基本使用

眼下主流App开发中,部分是以側滑菜单为主布局架构,曾经做android側滑菜单时。大多选择使用github上的第三方开源框架SildingMenu,可是这个框架还是稍显笨重。好消息是google已经开源了一个側滑菜单布局组件:DrawerLayout。DrawerLayout是V4包中的组件。也是直接继承于ViewGroup类。所以这个类也是一个容器类。使用DrawerLayout能够轻松的实现抽屉效果,使用DrawerLayout的步骤有下面1几点:

1)在DrawerLayout中,第一个子View必须是显示内容的view,而且设置它的layout_width和layout_height属性是match_parent.

2)第二个view是抽屉view,而且设置属性layout_gravity="left|right",表示是从左边滑出还是右边滑出。设置它的layout_height="match_parent"

<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=".MainActivity" > <TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="content" /> <ListView
android:id="@+id/listview"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#FFB5C5" /> </android.support.v4.widget.DrawerLayout>