前言

大家好,给大家带来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代码块:

<LinearLayout
android:orientation="vertical"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--标题栏-->
<include layout="@layout/main_title_bar" />
<!--放置Fragment的main_body-->
<RelativeLayout
android:id="@+id/main_body"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>

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

<!--实现在底部,水平排列按钮-->
<LinearLayout
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">
<RelativeLayout
android:layout_weight="1"
android:id="@+id/bottom_bar_1_btn"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
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"/>
<ImageView
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"/>
</RelativeLayout>
....<!--布局的代码总是繁琐又无聊的,记得要自己补全-->
</LinearLayout>

实例化控件

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

//先实例化控件,那我给出自己打的实例化代码
//来自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 即可

示例代码块

<?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:background="@android:color/white">
<TextView
android:text="Fragment_1"
android:textColor="@android:color/black"
android:textSize="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

然后通过我之前写的插件自动生成三个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知识点,有兴趣可以继续关注

AndroidStudio制作底部导航栏以及用Fragment实现切换功能的更多相关文章

  1. Android UI-仿微信底部导航栏布局

    现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...

  2. TextView+Fragment实现底部导航栏

    前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...

  3. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  4. Android学习笔记- Fragment实例 底部导航栏的实现

    1.要实现的效果图以及工程目录结构: 先看看效果图吧: 接着看看我们的工程的目录结构: 2.实现流程: Step 1:写下底部选项的一些资源文件 我们从图上可以看到,我们底部的每一项点击的时候都有不同 ...

  5. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  6. Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏

    在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示:   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...

  7. Flutter实战视频-移动电商-03.底部导航栏制作

    03.底部导航栏制作 material是谷歌退出的 还有另外的一种:cupertino是IOS的风格 我们底部的导航栏,静态的widget是不合适的,这垃圾我们用到动态的widget 这重新改成动态的 ...

  8. Android_ViewPager+Fragment实现页面滑动和底部导航栏

    1.Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager. <?xml version="1.0" encoding="utf-8" ...

  9. 03-Flutter移动电商实战-底部导航栏制作

    1.cupertino_IOS风格介绍 在Flutter里是有两种内置风格的: material风格: Material Design 是由 Google 推出的全新设计语言,这种设计语言是为手机.平 ...

随机推荐

  1. [译]与TensorFlow的第一次接触(三)之聚类

    转自 [译]与TensorFlow的第一次接触(三)之聚类 2016.08.09 16:58* 字数 4316 阅读 7916评论 5喜欢 18 前一章节中介绍的线性回归是一种监督学习算法,我们使用数 ...

  2. python 函数的名称空间及作用域

    一:名称空间 1:什么是名称空间: 名称空间即:储存名字与值的内存地址关联关系的空间 2.名称空间的分类: 内置名称空间:存储器自带的一些名称与值的对应关系,如:print,len,max等; 生命周 ...

  3. AD+DMA+USART实验中的收获和总结

    由于实验室用的是USART3接口,但是在基地实验时,由于没有RS232,只能换到USART1,进行实验.(在交作业的时候,记得要再换回去) 在这个过程中,遇到困难,用串口软件发送数据时无响应,应该意味 ...

  4. 《Network Security A Decision and Game Theoretic Approach》阅读笔记

    网络安全问题的背景 网络安全研究的内容包括很多方面,作者形象比喻为盲人摸象,不同领域的网络安全专家对网络安全的认识是不同的. For researchers in the field of crypt ...

  5. FOB cost---从工厂到码头的费用

    1.主要是港杂费:陆运400元起,2个方450元,三个方500元,3个方以上按100元/方算起.

  6. Mark,20180127,技术博客之路开启!

    不怎么会说话,自己本身少说话的撸码loner,少说多做.毕业从事手游一年多,之前主要从事Cocos2dx,刚转投Unity不到一个月,后面主要总结下自己在这两方面开发过程中的一些历程,希望自己能有所积 ...

  7. 450. Delete Node in a BST 删除bst中的一个节点

    [抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...

  8. Cannot resolve method

    1.问题描述: 本人idea下拉项目,结果impl里的类满屏的红色 2.解决方法 步骤: File --> Settings --> Plugins -->搜索并安装lom即可

  9. js学习(5)语法专题

    Js是一种动态类型语言,变量没有类型限制,可以随时赋值 强制转换: 主要指使用Number(),String()和Boolean()三个函数,手动将各个类型的值,分别转换为数字,字符串或布尔值 Num ...

  10. Android自定义View之上拉、下拉列表 头部元素跟随 缩放、平移效果的实现

    滑动ListView列表然后 listView上边的视图 跟随着上拉或者下拉的距离 自动放大或者缩小  视图里边元素自动平移的效果 思路很简单 根据listView 的滑动距离去计算图片和文字应该平移 ...