【Android Studio】实现底部导航栏Tab切换(提供Gitee源码)

news2024/9/20 14:30:31

前言:本期教学如何制作底部导航栏以及使用Fragment实现页面切换的完整功能,本篇提供所有源代码,均测试无误,大家可以放心使用。

目录

一、功能演示

二、代码实现

2.1、bottom.xml

2.2、message.xml、book.xml和mine.xml

2.3、activity_main.xml

2.4、MessageFragment、BookFragment和MineFragment

2.5、MainActivity

三、Gitee源码


一、功能演示

点击下方Tab栏目可以切换到对应的页面。

消息页面:

通讯录页面:

我的页面:

二、代码实现

先给前端代码,大家按顺序参考就行。

2.1、bottom.xml

先写底部的布局,这里面的图标可以自己网上随便找。

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#ECECEC"
    >

    <LinearLayout
        android:id="@+id/messageLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/messageImageView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/message"
            app:tint="#FFFFFF" />

        <TextView
            android:id="@+id/messageTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="消息"
            android:textColor="@color/white"
            android:textSize="24sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bookLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/bookImageView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/book"
            app:tint="#FFFFFF" />

        <TextView
            android:id="@+id/bookTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="通讯录"
            android:textColor="@color/white"
            android:textSize="24sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/mineLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/mineImageView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/mine"
            app:tint="#FFFFFF" />

        <TextView
            android:id="@+id/mineTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我的"
            android:textColor="@color/white"
            android:textSize="24sp" />

    </LinearLayout>


</LinearLayout>

预览:

2.2、message.xml、book.xml和mine.xml

这三个xml分别对应三个栏目所切换的页面,这边只贴出一个,其他两个复制一下,改一下xml文件名就行了。

完整代码:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="消息"
        android:textSize="54sp" />

</LinearLayout>

2.3、activity_main.xml

写一个FrameLayout用于动态显示页面,底部把之前写好的bottom.xml引入进来。

完整代码:

<?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:orientation="vertical">


    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <include
        layout="@layout/bottom"
        android:gravity="bottom" />

</LinearLayout>

下面展示后端java代码。 

2.4、MessageFragment、BookFragment和MineFragment

同样的我也是只给出一个完整代码,其他正常复制一下就行。

完整代码:

package com.example.tab;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MessageFragment extends Fragment {

    public MessageFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.message, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //页面初始化点击事件
    }

}

注: return inflater.inflate(R.layout.message, container, false);这边的R.layout.***复制的时候需要自己手动修改一下。

2.5、MainActivity

这个实体类主要就是实现点击切换的功能了。

完整代码:

package com.example.tab;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.tab.BookFragment;
import com.example.tab.MessageFragment;
import com.example.tab.MineFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private final Fragment messageFragment = new MessageFragment();

    private final Fragment bookFragment = new BookFragment();

    private final Fragment mineFragment = new MineFragment();
    private FragmentManager fragmentManager;
    private LinearLayout messageLayout,bookLayout,mineLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        messageLayout = findViewById(R.id.messageLayout);
        bookLayout = findViewById(R.id.bookLayout);
        mineLayout = findViewById(R.id.mineLayout);
        messageLayout.setOnClickListener(this);
        bookLayout.setOnClickListener(this);
        mineLayout.setOnClickListener(this);
        initFragment();

    }

    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        messageLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        bookLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        mineLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        transaction.add(R.id.frame_content,messageFragment);
        transaction.add(R.id.frame_content,bookFragment);
        transaction.add(R.id.frame_content,mineFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
        transaction.commit();
    }

    private void background(View v) {
        switch (v.getId()) {
            case R.id.messageLayout:
                messageLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            case R.id.bookLayout:
                bookLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            case R.id.mineLayout:
                mineLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            default:
                break;
        }
    }

    private void backgroundReturn(View v) {
        switch (v.getId()) {
            case R.id.messageLayout:
                messageLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            case R.id.bookLayout:
                bookLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            case R.id.mineLayout:
                mineLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            default:
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(messageFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
    }

    private void showFragment(int i) {

        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(messageFragment);
                background(messageLayout);
                backgroundReturn(bookLayout);
                backgroundReturn(mineLayout);
                break;
            case 1:
                transaction.show(bookFragment);
                background(bookLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(mineLayout);
                break;
            case 2:
                transaction.show(mineFragment);
                background(mineLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(bookLayout);
                break;
            default:
                break;
        }
        transaction.commit();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.messageLayout:
                showFragment(0);
                break;
            case R.id.bookLayout:
                showFragment(1);
                break;
            case R.id.mineLayout:
                showFragment(2);
                break;
            default:
                break;
        }
    }
}

大功告成.

三、Gitee源码

想要源代码的自行下载,博主免费提供: Android Studio实现底部导航栏Tab切换

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1923602.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第三期书生大模型实战营之Git前置知识

闯关任务1 每位参与者提交一份自我介绍。 提交地址&#xff1a;https://github.com/InternLM/Tutorial 的 camp3 分支&#xff5e; 要求 1. 命名格式为 camp3_<id>.md&#xff0c;其中 <id> 是您的报名问卷ID。 2. 文件路径应为 ./data/Git/task/。 3. 在 GitHub…

单网口设备的IP地址识别-还原-自组网

1.如果知道该设备所在网段&#xff1a; 此时可以使用nmap工具&#xff0c;进行网段扫描&#xff1a; nmap -sn 192.168.0.0/24 256个地址的子网10秒就能扫描一轮。关掉设备&#xff0c;打开设备&#xff0c;diff&#xff0c;基本就可以定位所要找到目标设备的IP 2.如果不知道…

链接追踪系列-04.linux服务器docker安装elk

[rootVM-24-17-centos ~]# cat /proc/sys/vm/max_map_count 65530 [rootVM-24-17-centos ~]# sysctl -w vm.max_map_count262144 vm.max_map_count 262144 #先创建出相应目录&#xff1a;/opt/dockerV/es/…docker run -e ES_JAVA_OPTS"-Xms512m -Xmx512m" -d -p 92…

隔离驱动-视频课笔记

目录 1、需要隔离的原因 1.2、四种常用的隔离方案 2、脉冲变压器隔离 2.1、脉冲变压器的工作原理 2.2、泄放电阻对开关电路的影响 2.3、本课小结 3、光耦隔离驱动 3.1、光耦隔离驱动原理 3.2、光耦隔离驱动的电源进行分析 3.3、本课小结 4、自举升压驱动 4.1…

哪款开放式运动耳机佩戴最舒服?2024五款备受推崇产品分享!

​热爱户外活动的你&#xff0c;定是对生活有着独到品味的行者。想象一下&#xff0c;在户外活动时&#xff0c;若有一款耳机能完美融入场景&#xff0c;为你带来无与伦比的音乐享受&#xff0c;岂不是锦上添花&#xff1f;此时&#xff0c;开放式耳机便应运而生&#xff0c;其…

SEO:6个避免被搜索引擎惩罚的策略-华媒舍

在当今数字时代&#xff0c;搜索引擎成为了绝大多数人获取信息和产品的首选工具。为了在搜索结果中获得良好的排名&#xff0c;许多网站采用了各种优化策略。有些策略可能会适得其反&#xff0c;引发搜索引擎的惩罚。以下是彭博社发稿推广的6个避免被搜索引擎惩罚的策略。 1. 内…

结合实体类型信息(3)——TransT: 基于类型的多重嵌入表示用于知识图谱补全

1 引言 1.1 问题 仅仅依赖于三元组的结构化信息有其局限性&#xff0c;因为它们往往忽略了知识图谱中丰富的语义信息以及由这些语义信息所代表的先验知识。语义信息是指实体和关系的含义&#xff0c;比如“北京”是“中国”的首都&#xff0c;“苹果”是一种水果。先验知识则…

uniapp编译成h5后接口请求参数变成[object object]

问题&#xff1a;uniapp编译成h5后接口请求参数变成[object object] 但是运行在开发者工具上没有一点问题 排查&#xff1a; 1&#xff1a;请求参数&#xff1a;看是否是在请求前就已经变成了[object object]了 结果&#xff1a; 一切正常 2&#xff1a;请求头&#xff1a;看…

yolov8-obb训练自己的数据集(标注,训练,推理,转化模型)

一、源码 直接去下载官方的yolov8源码就行&#xff0c;那里面集成了 obb ultralytics/ultralytics/cfg/models/v8 at main ultralytics/ultralytics GitHub 二、环境 如果你训练过yolov5以及以上的yolo环境&#xff0c;可以直接拷贝一个用就行&#xff0c;如果没有的话 直…

破解数据孤岛:论数据中台对企业数据治理的作用与挑战-亿发

在数字化转型浪潮中&#xff0c;数据中台这一概念频频被提及。然而&#xff0c;业界目前尚未对数据中台形成统一的定义。本文将基于PowerData的理解&#xff0c;深入探讨数据中台的核心价值与挑战。 数据中台的本质 数据中台不仅仅是一项单一的技术&#xff0c;而是涵盖数据集…

R语言中交互式图表绘制

revenue <- read.csv("data/revenue.csv") 数据集放在了文章末尾&#xff0c;需要自取。 if(!require(plotly)) install.packages("plotly") # 绘制柱状图 p <- plot_ly(revenue,y ~本周,x ~游戏名称,type "bar",name "本周&q…

记一次项目经历

一、项目需求 1、设备四个工位&#xff0c;每个工位需要测试产品的电参数&#xff1b; 2、每个另外加四个位置温度&#xff1b; 3、显示4个通道电流曲线&#xff0c;16个通道温度曲线&#xff1b; 4、可切换工艺参数&#xff1b; 5、常规判定&#xff0c;测试数据保存到表格内&…

AndoridStudio 使用 Inspect code 检查优化代码

日常开发时&#xff0c;AS 会有报黄提示&#xff0c;如果不修改&#xff0c;日积月累下来&#xff0c;应用性能就有问题了。 针对这种情况&#xff0c;可以使用 AS 自带的 Inspect code 功能来批量检查、优化代码。 选择 Code – Inspect Code &#xff0c; 按需选择 整个工…

如何允许从互联网(外网)进入路由器管理页面

1.绑定UDP端口 操作如图所示&#xff1a; 2.然后再绑定虚拟换回网卡 3.然后再把出端口编号设置成为2 使他成为一个双向输入输出具体操作如图所示&#xff1a; 4.进入防火墙然后再启动防火墙进行端口配置&#xff1a; 1.进入端口g0/0/0配置ip地址&#xff08;注意配置的ip地…

【web]-f12-iphone6

题目&#xff1a;屌丝没有苹果&#xff0c;手机都买不起&#xff0c;咋办&#xff1f;室友的iphone6好眼馋&#xff0c;某些网站也只有手机打得开(答案为flag{}形式&#xff0c;提交{}中内容即可) 手机模式浏览&#xff0c;F5刷新下就可以看到了。 flag a2a7c20140d7520903a70…

uniapp内置组件scroll-view案例解析

参考资料 文档地址&#xff1a;https://uniapp.dcloud.net.cn/component/scroll-view.html 官方给的完整代码 <script>export default {data() {return {scrollTop: 0,old: {scrollTop: 0}}},methods: {upper: function(e) {console.log(e)},lower: function(e) {cons…

MSPM0G3507(三十七)——最新资料包

所有代码本人全部试过都能用 &#xff0c;有啥疑问直接提出 推荐用软件OLED硬件6050&#xff0c;硬件6050读取速度较快&#xff0c;比较稳定 OLED是单独的纯OLED 两个6050程序分别为硬件6050软件oled&#xff0c;软件6050硬件OLED 全都是在CCStheia上编程&#xff0c;有啥问…

sentinel源码分析: dashboard与微服务的交互、pull模式持久化

文章目录 原始方式微服务端规则如何保存规则如何加载进内存微服务端接收控制台请求控制台推送规则总结 pull拉模式官方demo如何整合Spring Cloud整合Spring Cloud 前置知识 SentinelResource的实现原理、SphU.entry()方法中ProcessorSlotChain链、entry.exit() 建议先会使用se…

SvANet:微小医学目标分割网络,增强早期疾病检测

SvANet&#xff1a;微小医学目标分割网络&#xff0c;增强早期疾病检测 提出背景前人工作医学对象分割微小医学对象分割注意力机制 SvANet 结构图SvANet 解法拆解解法逻辑链 论文&#xff1a;SvANet: A Scale-variant Attention-based Network for Small Medical Object Segmen…

微博图片下载助手

开发的一款「微博图片下载助手」支持一键保存用户图片 / 原图保存 / 支持保存 live 动图&#xff0c;支持免登录&#xff0c;但是不支持去水印哦。另外软件是易语言编写的&#xff0c;一些杀毒软件可能会误报。 链接: https://pan.baidu.com/s/1ZwDuuS2AF0-nxGgYYPve_g?pwdwn…