谷歌官方推出了一種側(cè)滑菜單的實(shí)現(xiàn)方式(抽屜效果),即 DrawerLayout,這個(gè)類是在Support Library里的,需要加上android-support-v4.jar這個(gè)包。
為奎屯等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及奎屯網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、奎屯網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
使用注意點(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)容顯示頁背景的灰色?
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,很簡單
<?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è)簡單菜單
<?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)效果圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞名稱:Android開發(fā)之DrawerLayout實(shí)現(xiàn)抽屜效果
地址分享:http://bm7419.com/article12/pcidgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、做網(wǎng)站、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)