参考: 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>