package com.example.day1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Fragment01 f1;
private Fragment02 f2;
private Fragment03 f3;
private Fragment04 f4;
private RadioGroup ra;
private FrameLayout fram;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ra = (RadioGroup) findViewById(R.id.rd);
fram = (FrameLayout) findViewById(R.id.fram);
List<Fragment> list = new ArrayList<Fragment>();
f1 = new Fragment01();
f2 = new Fragment02();
f3 = new Fragment03();
f4 = new Fragment04();
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
final FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fram,list.get(0)).commit();
ra.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.radio1:
fragmentManager.beginTransaction().replace(R.id.fram,f1).commit();
break;
case R.id.radio2:
fragmentManager.beginTransaction().replace(R.id.fram,f2).commit();
break;
case R.id.radio3:
fragmentManager.beginTransaction().replace(R.id.fram,f3).commit();
break;
case R.id.radio4:
fragmentManager.beginTransaction().replace(R.id.fram,f4).commit();
break;
}
}
});
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Fragment01 f1;
private Fragment02 f2;
private Fragment03 f3;
private Fragment04 f4;
private RadioGroup ra;
private FrameLayout fram;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ra = (RadioGroup) findViewById(R.id.rd);
fram = (FrameLayout) findViewById(R.id.fram);
List<Fragment> list = new ArrayList<Fragment>();
f1 = new Fragment01();
f2 = new Fragment02();
f3 = new Fragment03();
f4 = new Fragment04();
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
final FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fram,list.get(0)).commit();
ra.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.radio1:
fragmentManager.beginTransaction().replace(R.id.fram,f1).commit();
break;
case R.id.radio2:
fragmentManager.beginTransaction().replace(R.id.fram,f2).commit();
break;
case R.id.radio3:
fragmentManager.beginTransaction().replace(R.id.fram,f3).commit();
break;
case R.id.radio4:
fragmentManager.beginTransaction().replace(R.id.fram,f4).commit();
break;
}
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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="com.example.administrator.myapplication.MainActivity01">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:id="@+id/fl"
></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:id="@+id/rg">
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/rg1"
android:button="@null"
android:layout_gravity="center"
android:gravity="center"
android:text="首页"
android:background="@drawable/selector"
android:checked="true"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/rg2"
android:button="@null"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/selector"
android:text="新闻"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/rg3"
android:button="@null"
android:text="我的"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/selector"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/rg4"
android:button="@null"
android:layout_gravity="center"
android:gravity="center"
android:text="咨询"
android:background="@drawable/selector"
/>
</RadioGroup>
</LinearLayout>