radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public?void?onCheckedChanged(RadioGroup?group,?int?checkedId)?{
//?TODO?Auto-generated?method?stub
switch?(checkedId)?{
case?R.id.but01:
//創(chuàng)建FragmentManager管理者
FragmentManager?fragmentManager=getSupportFragmentManager();
//通過FragmentManager開啟一個(gè)事務(wù)
FragmentTransaction?transaction=fragmentManager.beginTransaction();
//加載fragment??arg0??占位的控件??第二個(gè)參數(shù)是加載的Fragment
transaction.replace(R.id.fragment,?new?Fragment01());
//提交
transaction.commit();
break;
case?R.id.but02:
FragmentManager?fragmentManager2=getSupportFragmentManager();
FragmentTransaction?transaction2=fragmentManager2.beginTransaction();
transaction2.replace(R.id.fragment,?new?Fragment02());
transaction2.commit();
break;
case?R.id.but03:
FragmentManager?fragmentManager3=getSupportFragmentManager();
FragmentTransaction?transaction3=fragmentManager3.beginTransaction();
transaction3.replace(R.id.fragment,?new?Fragment03());
transaction3.commit();
break;
default:
break;
}
}
});
//////////////////////////////////////////////////////////fragment的布局
<FrameLayout
android:id="@+id/fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioButton
android:id="@+idbutton"
android:button="@null"
android:text="頭條"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"?/>
<RadioButton
android:id="@+idbutton2"
android:button="@null"
android:text="國內(nèi)"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"?/>
<RadioButton
android:id="@+idbutton3"
android:button="@null"
android:text="軍事"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"?/>
<RadioButton
android:id="@+idbutton4"
android:button="@null"
android:text="科技"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"?/>
</RadioGroup>