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