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>
相关推荐
Domain-zhuo1 天前
Git常用命令
前端·git·gitee·github·gitea·gitcode
行者彡3 天前
gitee别人仓库再上传自己仓库
gitee
sin22013 天前
git推送本地仓库到远程(Gitee)
git·gitee
o(╥﹏╥)4 天前
github如何给本机绑定 ssh密钥(MACOS)
运维·gitee·ssh·github
IT闫5 天前
【Git从入门到精通】——新版IDea集成Git、Idea集成Github、Gitee以及GItLab应用(看这一篇就够了)
git·gitee·gitlab·github·intellij-idea
忆枫呐♬8 天前
idea无法识别文件,如何把floder文件恢复成model
java·gitee·intellij-idea
Gin_Zou10 天前
通过Idea实现与gitee连接并拉取项目和上传
gitee·intellij-idea·源代码管理
峥嵘life10 天前
Android 系统应用重名install安装失败分析解决
android·gitee
爱喝矿泉水的猛男11 天前
在IDE中使用Git
ide·git·gitee
wy31362282111 天前
android——录制屏幕
android·gitee