【Android Studio】实现底部导航栏Tab切换(提供Gitee源码)
创始人
2025-01-15 07:03:28
0

前言:本期教学如何制作底部导航栏以及使用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

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

完整代码:

                                                                                                    

预览:

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

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

完整代码:

         

2.3、activity_main.xml

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

完整代码:

                      

下面展示后端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切换

相关内容

热门资讯

4分钟科普!蜀山四川智能辅助插... 4分钟科普!蜀山四川智能辅助插件(开挂)原来有挂脚本(分析开挂平台);蜀山四川智能辅助插件是一款益智...
玩家必看教程!家乡大贰小程序辅... 玩家必看教程!家乡大贰小程序辅助器(辅助)外挂开挂辅助软件(有挂教学)-哔哩哔哩;家乡大贰小程序辅助...
两分钟讲解!热血江湖手游辅助,... 两分钟讲解!热血江湖手游辅助,新道游正版辅助,揭秘教程(有挂详情)-哔哩哔哩>>您好:软件加薇136...
3分钟介绍!天天爱消除辅助脚本... 3分钟介绍!天天爱消除辅助脚本(透视)原来真的有挂神器(原来有开挂辅助器)相信很多朋友都在电脑上玩过...
三分钟了解!广东闲来辅助工具(... 三分钟了解!广东闲来辅助工具(辅助)外挂开挂辅助插件(确实有挂)-哔哩哔哩;最新版2026是一款经典...
第三分钟了解!新悠悠辅助微信,... 蜀山四川怎么提高胜率是一款专注玩家量身打造的游戏记牌类型软件,在蜀山四川怎么提高胜率这款游戏中我们可...
4分钟解说!微乐老友广东辅助器... 4分钟解说!微乐老友广东辅助器(开挂)原来真的有挂安装(分析开挂神器);微乐老友广东辅助器是一款益智...
实测教程!万能透视辅助器免费版... 实测教程!万能透视辅助器免费版(辅助)外挂开挂辅助安装(有挂透视)-哔哩哔哩;亲真的是有正版授权,小...
八分钟辅助!新道游房间,嗨动互... 八分钟辅助!新道游房间,嗨动互动辅助,可靠教程(存在有挂)-哔哩哔哩;亲,嗨动互动辅助这款游戏原来确...
六分钟了解!鹰潭至上饶脚本(透... 六分钟了解!鹰潭至上饶脚本(透视)原来是有挂的软件(传授开挂工具);鹰潭至上饶脚本软件透视开挂更新新...