RadioGroup RadioButton底部导航栏

复制代码
参考: https://blog.csdn.net/lu202032/article/details/117632709

activity_home.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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/nav_host_fragment_activity_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <!-- https://blog.csdn.net/lu202032/article/details/117632709 -->
    <RadioGroup
        android:id="@+id/ra_group"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/home_rb_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/ic_home_frag_home"
            android:drawablePadding="3dp"
            android:gravity="center"
            android:text="首页"
            android:textColor="@drawable/home_tv_tc_sel" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <RadioButton
                android:id="@+id/home_rb_bill"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:button="@null"
                android:drawableTop="@drawable/ic_home_frag_bill"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:text="账单"
                android:textColor="@drawable/home_tv_tc_sel"
                app:layout_constraintTop_toTopOf="parent" />

            <!-- 角标 -->
            <TextView
                android:id="@+id/home_rb_bill_badge"
                android:layout_width="5dp"
                android:layout_height="5dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="-28dp"
                android:background="@drawable/home_rb_bill_badge"
                android:gravity="center"
                android:minWidth="5dp"
                android:minHeight="5dp"
                android:textColor="#FFFFFF"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>


        <RadioButton
            android:id="@+id/home_rb_my"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/ic_home_frag_my"
            android:drawablePadding="3dp"
            android:gravity="center"
            android:text="我的"
            android:textColor="@drawable/home_tv_tc_sel" />

    </RadioGroup>
</LinearLayout>
复制代码
ic_home_frag_home
复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/ic_home_frag_home_sel" />
    <item android:drawable="@drawable/ic_home_frag_home_def" />
</selector>
复制代码
home_tv_tc_sel
复制代码
<!-- res/drawable/home_tab.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#FF0000" /> <!-- 选中状态的颜色 -->
    <item android:color="#808080" /> <!-- 默认状态的颜色 -->
</selector>

HomeActivity.java

复制代码
public class HomeActivity extends BaseActivity<ActivityHomeBinding> {


    private BroadcastReceiver receiver;
    private x1Fragment x1Fragment;
    private xxxFragment xxxFragment;
    private x2Fragment x2Fragment;
    private x1FragmentType currentFragment;

    public static void open(BaseActivity context) { 
        Intent starter = new Intent(context, HomeActivity.class); 
        context.startActivity(starter);
        context.finish();
    }

    @Override
    protected ActivityHomeBinding getViewBinding() {
        return ActivityHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initData() { 
         
    }
 

    @Override
    public void initView() {
        setFragment(x1FragmentType.home);
    }

    public void setFragment(x1FragmentType fragType) {
        //获取Fragment管理器

        FragmentManager mFragmentManager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
        //隐藏所有Fragment
        hideFragments(mTransaction);
        switch (fragType) {
            case home:
                if (x1Fragment == null) {
                    x1Fragment = new x1Fragment();
                    mTransaction.add(R.id.nav_host_fragment_activity_main, x1Fragment, "x1Fragment");
                } else {
                    mTransaction.show(x1Fragment);
                }
                break;
            case bill:
                if (xxxFragment == null) {
                    xxxFragment = new xxxFragment();
                    mTransaction.add(R.id.nav_host_fragment_activity_main, xxxFragment, "xxxFragment");
                } else {
                    mTransaction.show(xxxFragment);
                }
                break;
            case my:
                if (x2Fragment == null) {
                    x2Fragment = new x2Fragment();
                    mTransaction.add(R.id.nav_host_fragment_activity_main, x2Fragment, "x2Fragment");
                } else {
                    mTransaction.show(x2Fragment);
                }
                break;
            default:
                break;
        }
        //提交事务
        mTransaction.commitAllowingStateLoss();
        currentFragment = fragType;
    }

    //隐藏Fragment
    private void hideFragments(FragmentTransaction transaction) {
        if (xxxFragment != null) transaction.hide(xxxFragment);
        if (x1Fragment != null) transaction.hide(x1Fragment);
        if (x2Fragment != null) transaction.hide(x2Fragment);
    }

    @Override
    public void setListener() {
        mBinding.raGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.home_rb_home) {
                setFragment(x1FragmentType.home);
            } else if (checkedId == R.id.home_rb_bill) {
                setFragment(x1FragmentType.bill);
            } else if (checkedId == R.id.home_rb_my) {
                setFragment(x1FragmentType.my);
            }
        });
    } 
}

home_rb_bill_badge.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/badge_background.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#FF0000"/> <!-- 红色背景 -->
    <size
        android:width="20dp"
        android:height="20dp"/>
</shape>
相关推荐
deng-c-f5 小时前
配置(6):gitee如何远程上传文件
gitee
苗壮.1 天前
「个人 Gitee 仓库」与「企业 Gitee 仓库」同步的几种常见方式
大数据·elasticsearch·gitee
玥轩_5212 天前
Git命令速查手册
大数据·git·elasticsearch·gitee·github·命令速查
前端与小赵2 天前
uni-app开发安卓app时控制屏幕常亮不息屏
android·gitee·uni-app
我是好小孩3 天前
【Android】Binder 原理初探:理解 Android 进程通信机制
android·gitee·binder
mrsyf4 天前
VSCode的插件配置同步到gitee
vscode·gitee
generallizhong5 天前
android TAB切换
android·gitee
艾莉丝努力练剑6 天前
【Git:基本操作】深度解析Git:从初始Git到熟悉基本操作
大数据·linux·c++·人工智能·git·gitee·指令
YuanDaima20486 天前
GitHub 与 Gitee 多平台 SSH Key 配置指南
gitee·开源·ssh·github·开源软件·key·免密登录
TTGGGFF7 天前
开源项目分享:Gitee热榜项目 2025-10-28 日榜
gitee·开源