侧面菜单支持手机,平板

2023-11-11 04:59

本文主要是介绍侧面菜单支持手机,平板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

NI你现在顶部,底部导航栏太多了,我们几乎很少用到原生的Fragment了,是不是顶部与底部导航栏就可以满足所有需求呐?很显然不是,

我们知道平板比手机的尺寸要大得多,如果我们主页面利用底部导航栏设计出来空白太多,而且整体看起来不是很美观,于是乎我就想利用Fragment做一个侧面菜单。我在网上,Github,找了很多资料都没有合适的一个框架来支撑这个需求.最后我还是狠下心来用原生Fragment了。

1.首先我贴一张效果图,图片跟背景都是在随便找的资源.

 

我们再来看看上图对应的布局

?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@mipmap/back"><includeandroid:id="@+id/menu_layout"layout="@layout/main_menu"></include><TextViewandroid:id="@+id/title_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_toRightOf="@id/menu_layout"android:padding="10dp"android:text="亲睦家健康管理,欢迎您!"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"android:textColor="@color/normaltextcolor"/><FrameLayoutandroid:id="@+id/main_framelayout"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/title_text"android:layout_toRightOf="@id/menu_layout"android:background="@color/normaltextcolor"></FrameLayout></RelativeLayout>

main_menu布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="@dimen/x250"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loginlogo"/><de.hdodenhof.circleimageview.CircleImageViewandroid:id="@+id/main_menu_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:padding="@dimen/x5"android:src="@mipmap/error"/><TextViewandroid:id="@+id/menu_tologin"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_toRightOf="@id/main_menu_image"android:background="@null"android:layout_marginTop="@dimen/y10"android:text="登录/注册"android:textColor="@color/colorAccent"android:visibility="visible"/><TextViewandroid:id="@+id/menu_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y10"android:gravity="center_horizontal"android:text="昵称"android:textColor="@color/morencolor"/><TextViewandroid:id="@+id/menu_one"android:layout_width="@dimen/x250"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y40"android:drawablePadding="@dimen/x8"android:drawableTop="@mipmap/old_gaoweitwo"android:gravity="center"android:paddingBottom="@dimen/y5"android:paddingTop="@dimen/y5"android:text="我的设备"android:textColor="@color/morencolor"/><TextViewandroid:id="@+id/menu_two"android:layout_width="@dimen/x250"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y30"android:drawablePadding="@dimen/x8"android:drawableTop="@mipmap/old_goodsone"android:gravity="center"android:paddingBottom="@dimen/y5"android:paddingTop="@dimen/y5"android:text="设备列表"android:textColor="@color/morencolor"/><TextViewandroid:id="@+id/menu_three"android:layout_width="@dimen/x250"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y30"android:drawablePadding="@dimen/x8"android:drawableTop="@mipmap/old_leaveone"android:gravity="center"android:paddingBottom="@dimen/y5"android:paddingTop="@dimen/y5"android:text="其他"android:textColor="@color/morencolor"/><TextViewandroid:id="@+id/menu_four"android:layout_width="@dimen/x250"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y30"android:drawablePadding="@dimen/x8"android:drawableTop="@mipmap/old_messageone"android:gravity="center"android:paddingBottom="@dimen/y5"android:paddingTop="@dimen/y5"android:text="设置"android:textColor="@color/morencolor"/><TextViewandroid:id="@+id/menu_five"android:layout_width="@dimen/x250"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y30"android:drawablePadding="@dimen/x8"android:drawableTop="@mipmap/old_messageone"android:gravity="center"android:paddingBottom="@dimen/y5"android:paddingTop="@dimen/y5"android:text="退出"android:textColor="@color/morencolor"/>
</LinearLayout>

可以看到这个布局很简单左边一个menu布局右边就是一个FrameLayout.接着我们来看下怎么实现切换Fragment的,下面我就贴几个重要方法:

1.监听menu按钮事件

@Override
public void onClick(View v) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();switch (v.getId()) {case R.id.menu_one:SetFragment(fragmentList, fragmentTransaction, 0);SetTextViewColor(ids[0]);break;case R.id.menu_two:SetFragment(fragmentList, fragmentTransaction, 1);SetTextViewColor(ids[1]);break;case R.id.menu_three:SetFragment(fragmentList, fragmentTransaction, 2);SetTextViewColor(ids[2]);break;case R.id.menu_four:SetFragment(fragmentList, fragmentTransaction, 3);SetTextViewColor(ids[3]);break;}}
//切换fragment
public void SetFragment(List<Fragment> fragmentList, FragmentTransaction fragmentTransaction, int index) {for (int i = 0; i < fragmentList.size(); i++) {if (i == index) {if (fragmentList.get(i).isVisible() && fragmentList.get(i).isAdded()) {return;} else {fragmentTransaction.add(R.id.main_framelayout, fragmentList.get(i));}} else {if (fragmentList.get(i).isAdded()) {fragmentTransaction.remove(fragmentList.get(i));}}}fragmentTransaction.commit();}//--设置字体颜色:这里的ids是menu菜单中几个控件的id数组
public void SetTextViewColor(int id) {for (int i = 0; i < ids.length; i++) {TextView view = (TextView) findViewById(ids[i]);if (id == ids[i]) {view.setTextColor(Color.parseColor("#FF4081"));view.setTextSize(16);view.setBackgroundColor(Color.parseColor("#eeeeee"));Drawable drawable = getResources().getDrawable(imagestwo[i]);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());view.setCompoundDrawables(null, drawable, null, null);} else {view.setTextColor(Color.parseColor("#333333"));view.setTextSize(15);view.setBackground(null);Drawable drawable = getResources().getDrawable(images[i]);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());view.setCompoundDrawables(null, drawable, null, null);}}

温馨提示: 这里的Fragment管理栈不建议使用show(),hide()方法,因为这种主页面几乎没Activity什么事了,几乎所有的界面都由Fragment支撑,很显然用Show(),Hide()方法主页面Fragment栈管理会有很多的Fragment,这样会影响程序的性能问题.很可能导致程序崩溃.

这篇关于侧面菜单支持手机,平板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

Python实现自动化接收与处理手机验证码

《Python实现自动化接收与处理手机验证码》在移动互联网时代,短信验证码已成为身份验证、账号注册等环节的重要安全手段,本文将介绍如何利用Python实现验证码的自动接收,识别与转发,需要的可以参考下... 目录引言一、准备工作1.1 硬件与软件需求1.2 环境配置二、核心功能实现2.1 短信监听与获取2.

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

定价129元!支持双频 Wi-Fi 5的华为AX1路由器发布

《定价129元!支持双频Wi-Fi5的华为AX1路由器发布》华为上周推出了其最新的入门级Wi-Fi5路由器——华为路由AX1,建议零售价129元,这款路由器配置如何?详细请看下文介... 华为 Wi-Fi 5 路由 AX1 已正式开售,新品支持双频 1200 兆、配有四个千兆网口、提供可视化智能诊断功能,建

你的华为手机升级了吗? 鸿蒙NEXT多连推5.0.123版本变化颇多

《你的华为手机升级了吗?鸿蒙NEXT多连推5.0.123版本变化颇多》现在的手机系统更新可不仅仅是修修补补那么简单了,华为手机的鸿蒙系统最近可是动作频频,给用户们带来了不少惊喜... 为了让用户的使用体验变得很好,华为手机不仅发布了一系列给力的新机,还在操作系统方面进行了疯狂的发力。尤其是近期,不仅鸿蒙O

禁止平板,iPad长按弹出默认菜单事件

通过监控按下抬起时间差来禁止弹出事件,把以下代码写在要禁止的页面的页面加载事件里面即可     var date;document.addEventListener('touchstart', event => {date = new Date().getTime();});document.addEventListener('touchend', event => {if (new

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

Windows如何添加右键新建菜单

Windows如何添加右键新建菜单 文章目录 Windows如何添加右键新建菜单实验环境缘起以新建`.md`文件为例第一步第二步第三步 总结 实验环境 Windows7 缘起 因为我习惯用 Markdown 格式写文本,每次新建一个.txt后都要手动修改为.md,真的麻烦。如何在右键新建菜单中添加.md选项呢? 网上有很多方法,这些方法我都尝试了,要么太麻烦,要么不凑效