【基于Java语言的Android个人开发笔记,个人工具类笔记】App常见的五栏引导碎片基础写法

前期准备:一个主Activity,5个Fragment

主Activity
五个Fragment
我们在主Activity中添加碎片布局,和点击跳转按钮
这里我利用了单选组进行显示点击效果
文字点击效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#666" android:state_checked="false" />
    <item android:color="#E53935" android:state_checked="true" />
</selector>

可以在这里修改点击到颜色效果

    <item android:color="#666" android:state_checked="false" />
    <item android:color="#E53935" android:state_checked="true" />

主Activity的XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frame_mian"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        app:layout_constraintBottom_toTopOf="@+id/group_main"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></FrameLayout>

    <RadioGroup
        android:id="@+id/group_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints">

        <RadioButton
            android:id="@+id/radio_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="A"
            android:textColor="@drawable/text_selector" />

        <RadioButton
            android:id="@+id/radio_serve"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="B"
            android:textColor="@drawable/text_selector" />

        <RadioButton
            android:id="@+id/radio_more"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="C"
            android:textColor="@drawable/text_selector" />

        <RadioButton
            android:id="@+id/radio_new"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="D"
            android:textColor="@drawable/text_selector" />

        <RadioButton
            android:id="@+id/radio_my"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="E"
            android:textColor="@drawable/text_selector" />
    </RadioGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

主Activity的Java文件

    /**
     * 底部五个的按钮图标设置
     */
    private void initRadioButton() {
        Drawable drawable1 = getResources().getDrawable(R.drawable.first_selector);
        drawable1.setBounds(0, 0, 85, 85);
        radioA.setCompoundDrawables(null, drawable1, null, null);
        Drawable drawable2 = getResources().getDrawable(R.drawable.serve_selector);
        drawable2.setBounds(0, 0, 85, 85);
        radioB.setCompoundDrawables(null, drawable2, null, null);
        Drawable drawable3 = getResources().getDrawable(R.drawable.more_selector);
        drawable3.setBounds(0, 0, 85, 85);
        radioC.setCompoundDrawables(null, drawable3, null, null);
        Drawable drawable4 = getResources().getDrawable(R.drawable.new_selector);
        drawable4.setBounds(0, 0, 85, 85);
        radioD.setCompoundDrawables(null, drawable4, null, null);
        Drawable drawable5 = getResources().getDrawable(R.drawable.my_selector);
        drawable5.setBounds(0, 0, 85, 85);
       radioE.setCompoundDrawables(null, drawable5, null, null);
    }

    /**
     * 底部五个的按钮
     */
    private void initBtn() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian,new AFragment()).commit();
        groupMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_a:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian, new AFragment()).commit();
                        break;
                    case R.id.radio_b:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian, new BFragment()).commit();
                        break;
                    case R.id.radio_c:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian, new CFragment()).commit();
                        break;
                    case R.id.radio_d:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian, new DFragment()).commit();
                        break;
                    case R.id.radio_e:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_mian, new EFragment()).commit();
                        break;
                }
            }
        });
    }

碎片主要保留的代码

public class AFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //编写代码的主要位置
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值