第一步 在activity_main.xml中布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
//FrameLayout
<FrameLayout
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
//底部按钮
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="25dp"
android:background="@drawable/selector"
android:text="首页"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="25dp"
android:button="@null"
android:background="@drawable/selector"
android:text="推荐"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="25dp"
android:background="@drawable/selector"
android:text="我的"/>
</RadioGroup>
</LinearLayout>
第二步 在MainActivity.java中写代码
public class MainActivity extends AppCompatActivity {
private RadioGroup radiogroup;
private FragmentManager manager;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
//查找控件
radiogroup = (RadioGroup)findViewById( R.id.radiogroup );
//得到管理这类
manager = getSupportFragmentManager();
//开启事物
FragmentTransaction transaction = manager.beginTransaction();
//添加事物
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
transaction.add( R.id.pager, fragment1 );
transaction.add( R.id.pager, fragment2 );
transaction.add( R.id.pager, fragment3 );
//只显示第一个fragment
transaction.show( fragment1 ).hide( fragment2 ).hide( fragment3 );
//默认选中第一个按钮
radiogroup.check( radiogroup.getChildAt( 0 ).getId() );
//提交事物
transaction.commit();
//当我点击按钮时 页面切换
radiogroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction1 = manager.beginTransaction();
switch (checkedId){
case R.id.radio1:
//显示对应的fragment
transaction1.show( fragment1 ).hide( fragment2 ).hide( fragment3 );
break;
case R.id.radio2:
//显示对应的fragment
transaction1.show( fragment2 ).hide( fragment1 ).hide( fragment3 );
break;
case R.id.radio3:
//显示对应的fragment
transaction1.show( fragment3 ).hide( fragment2 ).hide( fragment1 );
break;
}
//提交事务
transaction1.commit();
}
} );//当我点击按钮时 页面切换
}
}