xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragme"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mgroup"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignBottom="@+id/fragme">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="button1"
android:layout_weight="1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="button1"
android:layout_weight="1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="button1"
android:layout_weight="1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:text="button1"
android:layout_weight="1"/>
</RadioGroup>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
JAVA代码实现
public class Main2Activity extends FragmentActivity {
private FrameLayout frameLayout;
private DrawerLayout drawerLayout;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
drawerLayout = findViewById(R.id.drawer);
frameLayout = findViewById(R.id.fragme);
group = findViewById(R.id.mgroup);
fragmentManager = getSupportFragmentManager();
transaction = fragmentManager.beginTransaction();
transaction.add(R.id.fragme,new Frag01());
transaction.add(R.id.fragme,new Frag02());
transaction.replace(R.id.fragme,new Frag01());
transaction.commit();
group.check(group.getChildAt(0).getId());
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
private FragmentTransaction transaction;
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
transaction = fragmentManager.beginTransaction();
switch (checkedId){
case R.id.button1:
transaction.replace(R.id.fragme,new Frag01());
break;
case R.id.button2:
transaction.replace(R.id.fragme,new Frag02());
break;
case R.id.button3:
transaction.replace(R.id.fragme,new Frag03());
break;
case R.id.button4:
transaction.replace(R.id.fragme,new Frag04());
break;
}
transaction.commit();
}
});
}
}