Android官方组件DrawerLayout简单使用
一、先写好布局main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_drawer"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/center_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主界面"
android:textSize="20sp"
android:textColor="#ffff00"/>
</LinearLayout>
<FrameLayout
android:id="@+id/left_content"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/image_button_portrait"
android:background="@drawable/ic_person_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/name"
android:textColor="#0000ff"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/image_button_setting"
android:background="@drawable/ic_settings_black_24dp"/>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
其中的第一个LinearLayout是主界面,后面的FrameLayout是侧滑栏中的内容(注意:DrawerLayout将第一个布局作为主界面,第二个布局作为侧滑栏的内容)。
注意其中的android:layout_gravity="start"是必须的,它将决定你使用的是从左划出还是从右划出(start代表从左向右划出,end代表从右向左滑出)
接下来就可以使用了,还有其他的功能自行去体会。