国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)之DrawerLayout實(shí)現(xiàn)抽屜效果

2019-10-23 18:27:36
字體:
供稿:網(wǎng)友

谷歌官方推出了一種側(cè)滑菜單的實(shí)現(xiàn)方式(抽屜效果),即 android/222207.html">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)容顯示頁背景的灰色?

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)效果圖

android,抽屜效果,Android開發(fā),DrawerLayout

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 隆子县| 和田市| 寻甸| 扎赉特旗| 武强县| 梅河口市| 牟定县| 永济市| 白银市| 互助| 申扎县| 黑河市| 酒泉市| 阿拉善盟| 莆田市| 苍溪县| 宁武县| 邢台县| 中宁县| 浦城县| 茶陵县| 鹤山市| 思茅市| 稻城县| 江口县| 交城县| 大关县| 宾阳县| 鄂伦春自治旗| 红河县| 徐汇区| 贡山| 嘉兴市| 贡山| 东阿县| 宣威市| 克拉玛依市| 柞水县| 札达县| 渝北区| 台前县|