DrawerLayout組件實現抽屜菜單

DrawerLayout組件一樣是V4包中的組件,也是直接繼承於ViewGroup類,因此這個類也是一個容器類。android

抽屜菜單的擺放和佈局經過android:layout_gravity屬性來控制,可選值爲left、right或start、end。經過xml來佈局的話,須要把DrawerLayout做爲父容器,組界面佈局做爲其第一個子節點,抽屜佈局則緊隨其後做爲第二個子節點,這樣就作就已經把內容展現區和抽屜菜單區獨立開來,只須要分別爲兩個區域設置內容便可。android提供了一些實用的監聽器,重載相關的回調方法能夠在菜單的交互過程當中書寫邏輯業務。佈局

使用DrawerLayout能夠輕鬆的實現抽屜效果,使用DrawerLayout的步驟有如下幾點:
spa

1)在DrawerLayout中,第一個子View必須是顯示內容的view,而且設置它的layout_width和layout_height屬性是match_parent..net

2)第二個view是抽屜view,而且設置屬性layout_gravity="left|right",表示是從左邊滑出仍是右邊滑出。設置它的layout_height="match_parent"code

eg:xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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>

實現的效果:繼承

相關文章
相關標籤/搜索