前言

大家好,我是 Vic,今天给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢

学习目标

AndroidStudio制作底部导航栏以及用Fragment实现切换功能,用户点击底部导航栏可以实现三个模块的跳转。

图片资源

需要底部导航栏三个点击按钮的图片资源

main_button_1.png,main_button_2.png,main_button_3.png

以及点击变换的图片资源

main_button_1_selected.png,

main_button_2_selected.png,

main_button_3_selected.png.

以上图片资源都放进drawable文件夹中

activity_main 布局

在 MainActivity 页面中主要有两个区域:

一个是放 Fragment 的 main_body

一个是放底部导航栏的 main_bottom_bar

主要的Fragment代码块:

android:orientation="vertical"

android:background="@android:color/white"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/main_body"

android:background="@android:color/white"

android:layout_width="match_parent"

android:layout_height="match_parent">

主要的底部导航栏的代码块:

android:id="@+id/main_bottom_bar"

android:layout_alignParentBottom="true"

android:background="#F2F2F2"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="55dp">

android:layout_weight="1"

android:id="@+id/bottom_bar_1_btn"

android:layout_width="0dp"

android:layout_height="match_parent">

android:id="@+id/bottom_bar_text_1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="3dp"

android:gravity="center"

android:singleLine="true"

android:text="button_1"

android:textColor="#666666"

android:textSize="14sp"/>

android:layout_width="27dp"

android:layout_height="27dp"

android:layout_above="@+id/bottom_bar_text_1"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="3dp"

android:id="@+id/bottom_bar_image_1"

android:src="@drawable/main_button_1"/>

....

实例化控件

实例化控件一些琐碎的代码:

//先实例化控件,那我给出自己打的实例化代码

//来自main_title_bar.xml

private TextView tv_main_title;//标题

private TextView tv_back;//返回按钮

private RelativeLayout title_bar;

//来自activity_main.xml

private RelativeLayout main_body;

private TextView bottom_bar_text_1;

private ImageView bottom_bar_image_1;

...

private LinearLayout main_bottom_bar;

private RelativeLayout bottom_bar_1_btn;

private RelativeLayout ...;

然后

initView();

//实例化

private void initView(){

//标题显示

tv_back=findViewById(R.id.tv_back);

tv_main_title=findViewById(R.id.tv_main_title);

title_bar=findViewById(R.id.title_bar);

//底部导航栏

main_body=findViewById(R.id.main_body);

bottom_bar_text_1=findViewById(R.id.bottom_bar_text_1);

bottom_bar_image_1=findViewById(R.id.bottom_bar_image_1);

...

//包含底部 android:id="@+id/main_bottom_bar"

main_bottom_bar=findViewById(R.id.main_bottom_bar);

//private RelativeLayout bottom_bar_1_btn;

bottom_bar_1_btn=findViewById(R.id.bottom_bar_1_btn);

//添加点击事件

bottom_bar_1_btn.setOnClickListener(this);

...

//技巧

//tv_back.setVisibility(View.GONE);

tv_main_title.setText("课程");

title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));

}

底部导航栏状态的切换方法

给MainActivity加一个setSelectStatus() 方法,方法里用参数index来判断当前选的按钮

示例代码

private void setSelectStatus(int index) {

switch (index){

case 0:

//图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原有的图片

bottom_bar_image_1.setImageResource(R.drawable.main_button_1_selected);

bottom_bar_text_1.setTextColor(Color.parseColor("#0097F7"));

//其他的文本颜色不变

bottom_bar_text_2.setTextColor(Color.parseColor("#666666"));

bottom_bar_text_3.setTextColor(Color.parseColor("#666666"));

//图片也不变

bottom_bar_image_2.setImageResource(R.drawable.main_button_2);

bottom_bar_image_3.setImageResource(R.drawable.main_button_3);

break;

case 1://同理如上

...

break;

case 2://同理如上

...

break;

}

}

实现底部导航栏的响应

导航栏文本颜色和图片切换效果的方法写好了,接下来是点击响应的方法

给MainActivity加上View.OnClickListener接口

在生成的 onClick() 方法中加上导航栏区域的响应

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.bottom_bar_1_btn:

setSelectStatus(0);

break;

case R.id.bottom_bar_2_btn:

setSelectStatus(1);

break;

case R.id.bottom_bar_3_btn:

setSelectStatus(2);

break;

}

}

别忘了在initView() 中添加监听器

bottom_bar_1_btn.setOnClickListener(this);

三个 fragment 的创建

就是简单的创建三个布局,展现Fragment_1/2/3 即可

示例代码块

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white">

android:text="Fragment_1"

android:textColor="@android:color/black"

android:textSize="50sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

然后通过我之前写的插件自动生成三个Fragemnt ,就可以了不用管生成的Fragement_1/2/3.java文件了,

插件文章

《 Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率 》

三个fragment的显示和切换

在MainActivity里把AppCompatActivity改为FragmentActivity

把Fragment加到Activity里的代码

通常用这个来展示,但是代码过长,我们来简化一下

/*

* FragmentManager manager = getSupportFragmentManager();

* FragmentTransaction transaction = manager.beginTransaction();

* transaction.add(R.id.main_body,new CourseFragment()).commit();

* */

我们先来添加一个setMain() 方法,来显示打开界面时,显示的初始页面

/用于打开初始页面

private void setMain() {

//getSupportFragmentManager() -> beginTransaction() -> add -> (R.id.main_boy,显示课程 new CourseFragment()

this.getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();

}

上面的代码中可以看到相对来说比较少,那我们就用这个,然后我们来实现点击底部导航栏来切换响应的fragment,我们在onClick()中添加即可。

case R.id.bottom_bar_1_btn:

//添加

getSupportFragmentManager().beginTransaction().replace(R.id.main_body,new Button_1Fragment()).commit();

setSelectStatus(0);

break;

如果觉得不错,那就点个赞吧!❤️

总结

本文讲了AndroidStudio制作底部导航栏以及用Fragment实现切换功能,界面的布局介绍,如果您还有更好地理解,欢迎沟通

定位:分享 Android&Java知识点,有兴趣可以继续关注

Android studio实现底部导航,AndroidStudio制作底部导航栏以及用Fragment实现切换功能...相关推荐

  1. android studio忘记密码界面,AndroidStudio制作“我”的界面,设置,修改密码,设置密保和找回密码...

    前言 大家好,我是 Vic,今天给大家带来AndroidStudio制作"我"的界面,设置,修改密码,设置密保和找回密码的概述,希望你们喜欢 学习目标 掌握修改密码功能的开发,和实 ...

  2. Android Studio 类微信界面的制作

    设计目标 使用Android Studio完成类微信的门户页面框架设计,APP包含4个tab页面.框架设计使用fragment,activity. 功能说明 界面的样式和微信打开后的界面相似 1点击底 ...

  3. android studio 新建工程慢,关于AndroidStudio新建与编译项目速度慢解决办法

    android第一次新建项目是,相关依赖包需要下载很久,至少半小时,因为网速问题,还会多次下载失败. 解决办法如下: 1.通过镜像将gradle-5.4.1-all.zip下载到本地:解压到文件夹:D ...

  4. android studio自定义类,为AndroidStudio设置自定义类注释

    我们在使用eclipse的时候,只要在类上面输入/**再按enter,就会出现类注释. package com.demo; /** * * @author chenjunxu * */ public ...

  5. android小游戏编程视频下载,【安卓游戏】使用 Android Studio 打包 RPG Maker MV 制作的游戏,生成 apk 安装包...

    RPG Maker MV 是一个简单易用的游戏制作软件,即使不会编程也能做出一个有模有样的游戏.但是它只能生成基于 Html5 的游戏文件,而不能直接生成安卓手机需要的 apk 文件,所以我们要自己想 ...

  6. Android Studio录制手机屏幕并制作GIF演示动画

    逛简书,知乎或者GitHub时,看到很多大神写的技术文章,文章中有很多GIF演示动画来展示其所写代码的效果,这些GIF演示动画生动.形象的展示了笔者所要表达的意图.平时喜欢写点博客的我也按捺不住了,于 ...

  7. android studio ddms 打开空白,AndroidStudio中如何打开DDMS-AndroidStudio打开DDMS的教程 - 河东软件园...

    Android Studio是余款Android的集成开发工具,作用类似于Eclipse软件.而DDMS则是一款调试工具,全称为:Dalvik Debug Monitor Service.它在Andr ...

  8. android studio 安装apk失败,AndroidStudio安装apk时失败时提示INSTALL_PARSE_FAILED_NO_CERTIFICATES...

    安装apk时提示INSTALL_PARSE_FAILED_NO_CERTIFICATES错误,说明要安装的apk未进行签名,需签名后方可安装. 在android studio中增加签名方法,直接在ap ...

  9. 【错误记录】Flutter 报错 ( Android Studio 中 main.dart 左侧不显示设备栏 )

    文章目录 一.报错信息 二.解决方案 一 ( 备选方案 ) 三.解决方案 二 ( 推荐方案 ) 一.报错信息 为了解决 [错误记录]Flutter 构建报错 ( Because xxx require ...

最新文章

  1. 阅读Book: MultiObjective using Evolutionary Algorithms (4) --- 3 种方法find Non-dominated set
  2. vim g s 对比
  3. html 里运行php文件,如何在HTML文件中运行PHP脚本
  4. 结构体中的malloc 与 free
  5. 算法-二分搜索-找出最大值和次大值
  6. 网页跳转-重定向-102.课时102.【Django视图高级】重定向详解(Av61533158,P102)
  7. 【鲲鹏来了】手把手教你在鲲鹏上使用编程语言——Java、Python
  8. arguments.callee弃用与webuploader
  9. 一. JVM发展史,运行时数据区域,四大引用
  10. eclipse中server location为灰色,不能修改
  11. Security+ 学习笔记20 身份证明
  12. numpy 中的nan和常用的统计方法
  13. mysql_affected_rows()、mysql_fetch_row、mysql_fetch_assoc
  14. 阿里面试失败后,一气之下我图解了Java中18把锁
  15. mac打开nh文件-cajviewer.dmg
  16. 风险预测模型_5分+整合多中心临床样本构建5分子胰腺癌预后模型
  17. 洛谷P4767 [IOI2000]邮局(决策单调DP,四边形不等式优化)
  18. 半年损失超20亿美元,区块链安全赛道被资本疯抢
  19. delphi 画图表,曲线图
  20. OpenGL ES 2.0 for Android教程(九):添加触摸反馈

热门文章

  1. 对于SpringMVC框架使用的时候出现“警告: No mapping found for HTTP request with URI [/login]”的问题解决方案...
  2. Selenium Webdriver ie 浏览器
  3. 14个支持响应式设计的流行前端开发框架
  4. 和菜鸟一起深入学习国嵌实验之简单Makefile
  5. macos -bash: yarn: command not found/-bash: cnpm: command not found
  6. Render errors:One or more layouts are missing the layout_width or layout_height attributes
  7. vscode vetur 不想标签属性老是转行 配置
  8. 计算机操作系统稳定性的因素有哪些,计算机操作系统期末重点复习汇编.docx
  9. linux提示链接层次太多,嵌入式linuxmusic播放器
  10. yii+php+当前目录,Yii常用路径方法总结