Android开发之DrawerLayout实现抽屉效果
作者:YungFan 发布时间:2023-09-30 03:40:59
谷歌官方推出了一种侧滑菜单的实现方式(抽屉效果),即 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>
实现效果图
来源:http://www.jianshu.com/p/f31cb996c369?utm_source=tuicool&utm_medium=referral
猜你喜欢
- forward_list 概述forward_list 是 C++ 11 新增的容器,它的实现为单链表。forward_list 是支持从容
- 前言:文件的上传和下载在日常开发中很是常见,那么这一功能是如何实现的呢,下面我给大家介绍一下实现条件:1、需要一个form标签,method
- 背景实际开发中,常常需要将比较复杂的 JSON 字符串转换为对应的 Java 对象。这里记录下解决方案。如下所示,是入侵事件检测得到的 JS
- 在Linux中创建一个新进程的唯一方法是使用fork()函数。fork()函数是Linux中一个非常重要的函数,和以往遇到的函数有一些区别,
- 起源flutter作为一个跨平台的框架,在绘制上体现出了它跨平台的良好性能.那么,它是如何从runApp()后 绘制上屏的呢?本文将与你一起
- 官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-a
- Java 数据库连接池详解数据库连接池的原理是:连接池基本的思想是在系统初始化的时候,将数据库连接作为对象存储在内存中,当用户需要访问数据库
- 一、简介ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便。比如:Task支持线程的
- Android中获取资源 id 及资源 id 的动态获取我们平时获取资源是通过 findViewById 方法进行的,比如我们常
- 一、什么是热部署?热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。二、什么是SpringBoot热部署?SpringBoot
- PS:本文包含了大部分strings函数的说明,并附带举例说明。本来想自己整理一下的,发现已经有前辈整理过了,就转了过来。修改了原文一些源码
- 1. 定义在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。2. 使用的
- GET请求不支持对象传参问题@GetMapping("/getByParam")String hello(Student
- 井字棋游戏要求在3乘3棋盘上,每行都相同或者每列都相同再或者对角线相同,则胜出.因此我们可以使用一个二维数组来表示棋盘,判断胜负只需要判断数
- 合成聚合复用原则合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP
- 本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的TextView。当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的
- 前言环境: flutter sdk v1.7.8+hotfix.3@stable对应 flutter engine: 54ad777f这里关
- 1 简介Solace是一个强大的实时性的事件驱动消息队列。本文将介绍如何在Spring中使用,虽然代码使用的是Spring Boot,但并没
- Java读取json数据并存入数据库1. pom依赖<dependency> &nbs
- 最近在做上传文件的服务,简单看了网上的教程。结合实践共享出代码。由于网上的大多数没有服务端的代码,这可不行呀,没服务端怎么调试呢。Ok,先上