DrawerLayout組件實現抽屜菜單

bebuzymzyge594 8年前發布 | 9K 次閱讀 Android開發 移動開發

來自: http://my.oschina.net/summerpxy/blog/211862


DrawerLayout組件同樣是V4包中的組件,也是直接繼承于ViewGroup類,所以這個類也是一個容器類。

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

使用DrawerLayout可以輕松的實現抽屜效果,使用DrawerLayout的步驟有以下幾點:

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

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

eg:

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>

實現的效果:

 

 本文由用戶 bebuzymzyge594 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!