布局
<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:layout_weight="9" android:id="@+id/fragment" ></FrameLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:id="@+id/radio" > <RadioButton android:layout_width="0dp" android:id="@+id/but1" android:button="@null" android:layout_weight="1" android:layout_marginLeft="25dp" android:text="首页" android:layout_height="match_parent" /> <RadioButton android:button="@null" android:layout_width="0dp" android:id="@+id/but2" android:layout_weight="1" android:text="本地" android:layout_height="match_parent" /> <RadioButton android:layout_width="0dp" android:id="@+id/but3" android:button="@null" android:layout_weight="1" android:text="直播" android:layout_height="match_parent" /> <RadioButton android:layout_width="0dp" android:id="@+id/but4" android:button="@null" android:layout_weight="1" android:text="订阅" android:layout_height="match_parent" /> <RadioButton android:layout_width="0dp" android:id="@+id/but5" android:layout_weight="1" android:button="@null" android:text="发现" android:layout_height="match_parent" /> </RadioGroup> </LinearLayout>
JAVA代码
radioGroup=findViewById(R.id.radio); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction beginTransaction = manager.beginTransaction(); beginTransaction.replace(R.id.fragment,new Fragment1()); beginTransaction.commit(); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.but1: FragmentManager manager1 = getSupportFragmentManager(); FragmentTransaction beginTransaction1 = manager1.beginTransaction(); beginTransaction1.replace(R.id.fragment,new Fragment1()); beginTransaction1.commit(); break; case R.id.but2: FragmentManager manager2 = getSupportFragmentManager(); FragmentTransaction beginTransaction2 = manager2.beginTransaction(); beginTransaction2.replace(R.id.fragment,new Fragment2()); beginTransaction2.commit(); break; case R.id.but3: FragmentManager manager3 = getSupportFragmentManager(); FragmentTransaction beginTransaction3 = manager3.beginTransaction(); beginTransaction3.replace(R.id.fragment,new Fragment3()); beginTransaction3.commit(); break; case R.id.but4: FragmentManager manager4 = getSupportFragmentManager(); FragmentTransaction beginTransaction4 = manager4.beginTransaction(); beginTransaction4.replace(R.id.fragment,new Fragment4()); beginTransaction4.commit(); break; case R.id.but5: FragmentManager manager5 = getSupportFragmentManager(); FragmentTransaction beginTransaction5 = manager5.beginTransaction(); beginTransaction5.replace(R.id.fragment,new Fragment5()); beginTransaction5.commit(); break; default: break; } } });