Main方法
public class Main2Activity extends AppCompatActivity {
private List<Fragment> list;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
private Fragment5 fragment5;
private FrameLayout frame_layout;
private RadioGroup radio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
/*Intent intent1 = new Intent(Main2Activity.this, Service.class);
stopService(intent1);*/
list = new ArrayList<Fragment>();
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
fragment4 = new Fragment4();
fragment5 = new Fragment5();
list.add(fragment1);
list.add(fragment2);
list.add(fragment3);
list.add(fragment4);
list.add(fragment5);
final FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.frame_layout,list.get(0)).commit();
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.but01:
fragmentManager.beginTransaction().replace(R.id.frame_layout,fragment1).commit();
break;
case R.id.but02:
fragmentManager.beginTransaction().replace(R.id.frame_layout,fragment2).commit();
break;
case R.id.but03:
fragmentManager.beginTransaction().replace(R.id.frame_layout,fragment3).commit();
break;
case R.id.but04:
fragmentManager.beginTransaction().replace(R.id.frame_layout,fragment4).commit();
break;
case R.id.but05:
fragmentManager.beginTransaction().replace(R.id.frame_layout,fragment5).commit();
break;
}
}
});
}
private void initView() {
frame_layout = (FrameLayout) findViewById(R.id.frame_layout);
radio = (RadioGroup) findViewById(R.id.radio);
}
}
XML方法
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<RadioGroup
android:id="@+id/radio"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/but01"
android:layout_weight="1"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:checked="true"
android:background="@drawable/shouye"
android:text="首页"
/>
<RadioButton
android:id="@+id/but02"
android:layout_weight="1"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:checked="true"
android:background="@drawable/shiping"
android:text="视频"
/>
<RadioButton
android:id="@+id/but03"
android:layout_weight="1"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:checked="true"
android:background="@drawable/selector"
android:text="发布"
/>
<RadioButton
android:id="@+id/but04"
android:layout_weight="1"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:checked="true"
android:background="@drawable/toutiao"
android:text="微头条"
/>
<RadioButton
android:id="@+id/but05"
android:layout_weight="1"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:checked="true"
android:background="@drawable/wode"
android:text="我的"
/>
</RadioGroup>
</LinearLayout>