Android組件之DrawerLayout實現(xiàn)抽屜菜單
DrawerLayout組件同樣是V4包中的組件,也是直接繼承于ViewGroup類,所以這個類也是一個容器類。
抽屜菜單的擺放和布局通過android:layout_gravity屬性來控制,可選值為left、right或start、end。通過xml來布局的話,需要把DrawerLayout作為父容器,組界面布局作為其第一個子節(jié)點,抽屜布局則緊隨其后作為第二個子節(jié)點,這樣就做就已經(jīng)把內(nèi)容展示區(qū)和抽屜菜單區(qū)獨立開來,只需要分別為兩個區(qū)域設(shè)置內(nèi)容即可。android提供了一些實用的監(jiān)聽器,重載相關(guān)的回調(diào)方法可以在菜單的交互過程中書寫邏輯業(yè)務(wù)。
使用DrawerLayout可以輕松的實現(xiàn)抽屜效果,使用DrawerLayout的步驟有以下幾點:
1)在DrawerLayout中,第一個子View必須是顯示內(nèi)容的view,并且設(shè)置它的layout_width和layout_height屬性是match_parent.
2)第二個view是抽屜view,并且設(shè)置屬性layout_gravity="left|right",表示是從左邊滑出還是右邊滑出。設(shè)置它的layout_height="match_parent"
eg:
<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>
實現(xiàn)的效果:
以上所述是小編給大家介紹的Android組件之DrawerLayout實現(xiàn)抽屜菜單的相關(guān)知識,希望對大家有所幫助。
相關(guān)文章
Android shell命令行中過濾adb logcat輸出的幾種方法
本文主要介紹Android shell命令行中過濾adb logcat輸出的方法,這里整理了幾種方法,并詳細的說明,有需要的朋友可以參考下2016-08-08Android開發(fā)5:應(yīng)用程序窗口小部件App Widgets的實現(xiàn)(附demo)
本篇文章主要介紹了android應(yīng)用程序窗口小部件App Widgets的實現(xiàn),具有一定的參考價值,有需要的可以了解一下。2016-11-11詳解Android中fragment和viewpager的那點事兒
本文主要對Android中fragment和viewpager進行詳細介紹,具有一定的參考價值,需要的朋友一起來看下吧2016-12-12Android?ViewPager你可能不知道的刷新操作分享
這篇文章主要為大家詳細介紹了Android中ViewPager你可能不知道的刷新操作,文中的示例代碼講解詳細,具有一定的學(xué)習價值,需要的可以參考一下2023-05-05Android使用Sensor感應(yīng)器獲取用戶移動方向(指南針原理)
這篇文章主要介紹了Android使用Sensor感應(yīng)器獲取用戶移動方向的方法,實例分析了指南針原理極其應(yīng)用,需要的朋友可以參考下2015-12-12