当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之DrawerLayout实现抽屉效果

Android开发之DrawerLayout实现抽屉效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

春哥经,汉中贴吧,安康社区

谷歌官方推出了一种侧滑菜单的实现方式(抽屉效果),即 drawerlayout,这个类是在support library里的,需要加上android-support-v4.jar这个包。

使用注意点

1、drawerlayout的第一个子元素必须是默认内容,即抽屉没有打开时显示的布局(如framelayout),后面紧跟的子元素是抽屉内容,即抽屉布局(如listview)。

2、抽屉菜单的摆放和布局通过android:layout_gravity属性来控制,可选值为left、right或start、end。

3、抽屉菜单的宽度为 dp 单位而高度和父view一样。抽屉菜单的宽度应该不超过320dp,这样用户可以在菜单打开的时候看到部分内容界面。

4、打开抽屉: drawerlayout .opendrawer(); 关闭抽屉:drawerlayout.closedrawer( );

一个典型的布局实例:

<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">
  <!--可以在程序中根据抽屉菜单 切换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,下面会讲配合 android m推出的navigationview

遇到的问题

1、在点击drawerlayout中的空白处的时候,底部的content会获得事件。

由于google的demo是一个listview,所以listview会获得焦点,事件就不会传递了,看不出来问题。但是如果用的include加载的布局,会出现这个情况,那么如何解决?

解决办法:在include进的那个布局里面,添加clickable=true

2、除了抽屉的布局视图之外的视图究竟放哪里

左、右抽屉和中间内容视图默认是不显示的,其他布局视图都会直接显示出来,但是需要将其放在 drawerlayout 内部才能正常使用(不要放在外面),否则要么是相互覆盖,或者就是触屏事件失效,滚动等效果全部失效。

3、去除左右抽屉划出后内容显示页背景的灰色?

drawerlayout.setscrimcolor(color.transparent);

4、如何填充抽屉的划出后与屏幕边缘之间的内容(即上面的灰色部分)?

drawerlayout.setdrawershadow(drawable shadowdrawable, int gravity)

drawerlayout.setdrawershadow(int resid, int gravity)

配合navigationview实现抽屉菜单

navigationview是android m中提出一个新的md风格的组件,它将自己一分为二,上面显示一个通用的布局,下面显示一组菜单。与drawerlayout一起使用可以实现通用的侧滑菜单,布局如下

<?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" //左侧菜单
    app:headerlayout="@layout/header" //导航的顶部视图
    app:menu="@menu/menu_drawer_left" /> //导航的底部菜单
</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" //设置一下头部高度
  android:background="#123456" //设置一个背景色
  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,就构造四个简单菜单

<?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>


实现效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网