Android中DrawerLayout(仿QQ双向侧滑菜单效果)

2024-03-20 18:48

本文主要是介绍Android中DrawerLayout(仿QQ双向侧滑菜单效果),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android中DrawerLayout抽屉(仿QQ效果)
导航抽屉显示在屏幕的最左侧,默认情况下是隐藏的,当用户用手指从边缘向另一个滑动的时候,会出现一个隐藏的面板,当点击面板外部或者向原来的方向滑动的时候,抽屉导航就会消失了!
这个抽屉就是DrawerLayout,DrawerLayout是存在于v4包下的类,主要实现Android抽屉的效果。
重点内容
1.主内容视图一定要是DrawerLayout的第一个子视图
2.主内容视图宽度和高度需要match_parent
3.必须显示指定侧滑视图的android:layout_gravity属性
android:layout_gravity = “start”时,从左向右滑出菜单,android:layout_gravity = “end”时,从右向左滑出菜单

下面别的不多说,直接贴代码。

这里写代码片
xml布局:

这里写代码片
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/drawerLayout"android:background="@drawable/img_frame_background"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/home" ><Buttonandroid:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentRight="true"android:background="@drawable/btn"android:onClick="click" /></RelativeLayout><fragment android:name="com.example.cn.bgs.drawerlayoutqqdemo.FragmentA"android:layout_width="200dp"android:layout_height="match_parent"android:layout_gravity="start"android:tag="LEFT"/><fragment android:name="com.example.cn.bgs.drawerlayoutqqdemo.FragmentB"android:layout_width="100dp"android:layout_height="match_parent"android:layout_gravity="end"android:tag="RIGHT"/>
</android.support.v4.widget.DrawerLayout>

fragment_a 布局代码

这里写代码片
<?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="match_parent"android:gravity="center"android:orientation="vertical" ><LinearLayout
        android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:orientation="horizontal" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="10dp"android:src="@drawable/img_1" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="第一个item"android:textColor="#ffffff"android:textSize="18sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:orientation="horizontal" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="10dp"android:src="@drawable/img_2" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="第二个item"android:textColor="#ffffff"android:textSize="18sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:orientation="horizontal" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="10dp"android:src="@drawable/img_3" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="第三个item"android:textColor="#ffffff"android:textSize="18sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:orientation="horizontal" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="10dp"android:src="@drawable/img_4" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="第四个item"android:textColor="#ffffff"android:textSize="18sp" /></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="60dp"android:orientation="horizontal"android:gravity="center"><ImageView android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/img_5"android:layout_marginLeft="10dp"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第五个item"android:textColor="#ffffff"android:textSize="18sp"android:layout_marginLeft="5dp"/></LinearLayout></LinearLayout>

fragment_b 布局代码

这里写代码片
<?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="match_parent"android:padding="5dp"android:orientation="vertical" ><LinearLayout
        android:layout_width="match_parent"android:layout_height="70dp"android:gravity="center"android:orientation="vertical" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/saoma" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="扫一扫"android:textSize="15sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="70dp"android:gravity="center"android:orientation="vertical" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/phone" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="电话"android:textSize="15sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="70dp"android:gravity="center"android:orientation="vertical" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/person" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="联系人"android:textSize="15sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="70dp"android:gravity="center"android:orientation="vertical" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/mobile" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="手机"android:textSize="15sp" /></LinearLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="70dp"android:gravity="center"android:orientation="vertical" ><ImageView
            android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/discuss" /><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="讨论"android:textSize="15sp" /></LinearLayout>
</LinearLayout>

FragmentA 代码

这里写代码片
public class FragmentA extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_a, container, false);
}

}
FragmentB代码同理:

public class FragmentB extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_b, container, false);
}

}
MainActivity 代码

这里写代码片
public class MainActivity extends FragmentActivity implements DrawerListener {private DrawerLayout drawerLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);drawerLayout.setDrawerListener(this);drawerLayout.setScrimColor(color.transparent);drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.END);}public void click(View v) {drawerLayout.openDrawer(Gravity.END);drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.END);}@Overridepublic void onDrawerClosed(View arg0) {drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.END);}@Overridepublic void onDrawerOpened(View arg0) {}@Overridepublic void onDrawerSlide(View arg0, float arg1) {// 获得主视图的第一个View mContent = drawerLayout.getChildAt(0);// 获得菜单(抽屉)当前滑动的菜单View mMenu = arg0;// 缩放后的比列float scale = 1 - arg1;// 获取滑出菜单的宽度int mMenuWidth = drawerLayout.getMeasuredWidth();float leftscale = 0.8f + scale * 0.2f;// 内容区域ViewHelper.setScaleX(mContent, leftscale);ViewHelper.setScaleY(mContent, leftscale);// 抽屉区域ViewHelper.setScaleX(mMenu, 0.7f + 0.3f * arg1);ViewHelper.setScaleY(mMenu, 0.7f + 0.3f * arg1);ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * arg1);// 渐变动画效果if (arg0.getTag().equals("LEFT")) {ViewHelper.setTranslationX(mContent, mMenuWidth * arg1);// 位移动画效果} else {ViewHelper.setTranslationX(mContent, -mMenuWidth * arg1);}}@Overridepublic void onDrawerStateChanged(int arg0) {}}

完整的代码就写完了,下面看一下效果。
如图:
图一:

这里写图片描述

图二:

这里写图片描述

图三:

这里写图片描述

如有雷同,纯属意外。如果我的文章对你有帮助,请顶一下,谢谢,多多指教。本人将会继续写下去,,,,,,

这篇关于Android中DrawerLayout(仿QQ双向侧滑菜单效果)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830408

相关文章

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2