radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.but01:
//创建FragmentManager管理者
FragmentManager fragmentManager=getSupportFragmentManager();
//通过FragmentManager开启一个事务
FragmentTransaction transaction=fragmentManager.beginTransaction();
//加载fragment arg0 占位的控件 第二个参数是加载的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="国内"
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>
网友评论