FrameLayout类型的

在这里插入图片描述

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/fl"
        android:layout_weight="9">
    </FrameLayout>
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:button="@null"
            android:layout_weight="1"
            android:checked="true"
            android:textColor="#fff"
            android:background="@drawable/ic_launcher_background"
            android:text="开始"
            android:gravity="center"
            android:textSize="25sp"/>
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:button="@null"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@drawable/ic_launcher_background"
            android:text="首页"
            android:gravity="center"
            android:textSize="25sp"/>

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:textColor="#fff"
            android:background="@drawable/ic_launcher_background"
            android:text="购物车"
            android:textSize="25sp" />
        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:button="@null"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@drawable/ic_launcher_background"
            android:text="我的"
            android:gravity="center"
            android:textSize="25sp"/>
    </RadioGroup>


</LinearLayout>

在java界面

public class MainActivity extends AppCompatActivity{
    private FrameLayout f1;
    private RadioGroup rg;
    private frag_one one;
    private frag_two two;
    private frag_three three;
    private frag_four four;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化组件
        f1=findViewById(R.id.fl);
        rg=findViewById(R.id.rg);
        one=new frag_one();
        two=new frag_two();
        three=new frag_three();
        four=new frag_four();
        final FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl,one);
        transaction.add(R.id.fl,two);
        transaction.add(R.id.fl,three);
        transaction.add(R.id.fl,four);
        //进行显隐切换
        transaction.show(one).hide(two).hide(three).hide(four).commit();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        FragmentTransaction transaction1=getSupportFragmentManager().beginTransaction();
                        transaction1.show(one).hide(two).hide(three).hide(four).commit();
                        break;
                    case R.id.rb2:
                        FragmentTransaction transaction2=getSupportFragmentManager().beginTransaction();
                        transaction2.show(two).hide(one).hide(three).hide(four).commit();
                        break;
                    case R.id.rb3:
                        FragmentTransaction transaction3=getSupportFragmentManager().beginTransaction();
                        transaction3.show(three).hide(two).hide(one).hide(four).commit();
                        break;
                    case R.id.rb4:
                        FragmentTransaction transaction4=getSupportFragmentManager().beginTransaction();
                        transaction4.show(four).hide(two).hide(one).hide(three).commit();
                        break;
                }
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42828101/article/details/83899859