侧面菜单支持手机,平板

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

相关文章

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

Prometheus+cpolar如何在手机上也能监控服务器状态?

《Prometheus+cpolar如何在手机上也能监控服务器状态?》本文强调了通过Cpolar这一内网穿透工具,轻松突破Prometheus仅限于局域网访问的限制,实现外网随时随地访问监控数据,教你... 目录前言1.安装prometheus2.安装cpolar实现随时随地开发3.配置公网地址4.保留固定

golang实现nacos获取配置和服务注册-支持集群详解

《golang实现nacos获取配置和服务注册-支持集群详解》文章介绍了如何在Go语言中使用Nacos获取配置和服务注册,支持集群初始化,客户端结构体中的IpAddresses可以配置多个地址,新客户... 目录golang nacos获取配置和服务注册-支持集群初始化客户端可选参数配置new一个客户端 支

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

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

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

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

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

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

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