Android開發(fā)之DrawerLayout實(shí)現(xiàn)抽屜效果
谷歌官方推出了一種側(cè)滑菜單的實(shí)現(xiàn)方式(抽屜效果),即 DrawerLayout,這個(gè)類是在Support Library里的,需要加上android-support-v4.jar這個(gè)包。
使用注意點(diǎn)
1、DrawerLayout的第一個(gè)子元素必須是默認(rèn)內(nèi)容,即抽屜沒有打開時(shí)顯示的布局(如FrameLayout),后面緊跟的子元素是抽屜內(nèi)容,即抽屜布局(如ListView)。
2、抽屜菜單的擺放和布局通過android:layout_gravity屬性來控制,可選值為left、right或start、end。
3、抽屜菜單的寬度為 dp 單位而高度和父View一樣。抽屜菜單的寬度應(yīng)該不超過320dp,這樣用戶可以在菜單打開的時(shí)候看到部分內(nèi)容界面。
4、打開抽屜: DrawerLayout .openDrawer(); 關(guān)閉抽屜:DrawerLayout.closeDrawer( );
一個(gè)典型的布局實(shí)例:
<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!--可以在程序中根據(jù)抽屜菜單 切換Fragment--> <FrameLayout android:id="@+id/fragment_layout" android:background="#0000ff" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> <!--左邊抽屜菜單--> <RelativeLayout android:id="@+id/menu_layout_left" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ff0000"> <ListView android:id="@+id/menu_listView_l" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout> <!--右邊抽屜菜單--> <RelativeLayout android:id="@+id/menu_layout_right" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#00ff00"> <ListView android:id="@+id/menu_listView_r" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
這里存放的是ListView,下面會(huì)講配合 Android M推出的NavigationView
遇到的問題
1、在點(diǎn)擊DrawerLayout中的空白處的時(shí)候,底部的content會(huì)獲得事件。
由于Google的demo是一個(gè)ListView,所以ListView會(huì)獲得焦點(diǎn),事件就不會(huì)傳遞了,看不出來問題。但是如果用的include加載的布局,會(huì)出現(xiàn)這個(gè)情況,那么如何解決?
解決辦法:在include進(jìn)的那個(gè)布局里面,添加clickable=true
2、除了抽屜的布局視圖之外的視圖究竟放哪里
左、右抽屜和中間內(nèi)容視圖默認(rèn)是不顯示的,其他布局視圖都會(huì)直接顯示出來,但是需要將其放在 DrawerLayout 內(nèi)部才能正常使用(不要放在外面),否則要么是相互覆蓋,或者就是觸屏事件失效,滾動(dòng)等效果全部失效。
3、去除左右抽屜劃出后內(nèi)容顯示頁(yè)背景的灰色?
drawerLayout.setScrimColor(Color.TRANSPARENT);
4、如何填充抽屜的劃出后與屏幕邊緣之間的內(nèi)容(即上面的灰色部分)?
drawerLayout.setDrawerShadow(Drawable shadowDrawable, int gravity)
drawerLayout.setDrawerShadow(int resId, int gravity)
配合NavigationView實(shí)現(xiàn)抽屜菜單
NavigationView是Android M中提出一個(gè)新的MD風(fēng)格的組件,它將自己一分為二,上面顯示一個(gè)通用的布局,下面顯示一組菜單。與DrawerLayout一起使用可以實(shí)現(xiàn)通用的側(cè)滑菜單,布局如下
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/id_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Hello World" android:textSize="30sp" /> </RelativeLayout> <android.support.design.widget.NavigationView android:id="@+id/nv_menu_left" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" //左側(cè)菜單 app:headerLayout="@layout/header" //導(dǎo)航的頂部視圖 app:menu="@menu/menu_drawer_left" /> //導(dǎo)航的底部菜單 </android.support.v4.widget.DrawerLayout>
header.xml,很簡(jiǎn)單
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="240dp" //設(shè)置一下頭部高度 android:background="#123456" //設(shè)置一個(gè)背景色 android:orientation="vertical" android:padding="16dp"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_marginBottom="16dp" android:layout_marginTop="36dp" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="YungFan" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="http://www.jianshu.com/users/ab557ce505cd/timeline" /> </LinearLayout>
menu_drawer_left.xml,就構(gòu)造四個(gè)簡(jiǎn)單菜單
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/nav_home" android:icon="@mipmap/infusion" android:title="Home" /> <item android:id="@+id/nav_messages" android:icon="@mipmap/mypatient" android:title="Messages" /> <item android:id="@+id/nav_friends" android:icon="@mipmap/mywork" android:title="Friends" /> <item android:id="@+id/nav_discussion" android:icon="@mipmap/personal" android:title="Discussion" /> </menu>
實(shí)現(xiàn)效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
這篇文章主要介紹了Android動(dòng)態(tài)添加設(shè)置布局與控件的方法,涉及Android中布局與控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12解析后臺(tái)進(jìn)程對(duì)Android性能影響的詳解
本篇文章是對(duì)Android中后臺(tái)進(jìn)程對(duì)Android性能的影響進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法
這篇文章主要介紹了Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果
本文給大家分享Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果的代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-05-05