簡單效果:

效果
fragment_radio.xml主要代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radio_fragment_content"
android:layout_above="@+id/radio">
</FrameLayout>
<LinearLayout
android:id="@+id/radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
style="@style/radiobutton_style"
android:checked="true"
android:drawableTop="@drawable/ic_home"
android:text="主頁" />
<RadioButton
android:id="@+id/rb_like"
style="@style/radiobutton_style"
android:drawableTop="@drawable/ic_like"
android:text="喜歡"/>
<RadioButton
android:id="@+id/rb_me"
style="@style/radiobutton_style"
android:drawableTop="@drawable/ic_person"
android:text="我"/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
頁面為RadioFragment
public class RadioFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
@BindView(R.id.rb_home)
RadioButton rbHome;
@BindView(R.id.rb_like)
RadioButton rbLike;
@BindView(R.id.rb_me)
RadioButton rbMe;
@BindView(R.id.radio_group)
RadioGroup radioGroup;
Unbinder unbinder;
private HomeFragment homeFragment;
private LikeFragment likeFragment;
private MeFragment meFragment;
public RadioFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_radio, container, false);
unbinder = ButterKnife.bind(this, view);
radioGroup.setOnCheckedChangeListener(this);//對radiogroup進行監(jiān)聽
return view;
}
@Override
public void onStart() {
setDefaultFragment();//寫在onCreateView里面,當(dāng)頁面跑到其他Fragment再回來就不會生效
super.onStart();
}
private void setDefaultFragment() {
//默認為homeFragment
if (rbHome.isChecked()) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
homeFragment = new HomeFragment();
transaction.replace(R.id.radio_fragment_content, homeFragment).commit();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
//主要代碼
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
FragmentTransaction transaction=getFragmentManager().beginTransaction();
switch (i){
case R.id.rb_home:
if (homeFragment==null){
homeFragment= new HomeFragment();
}
transaction.replace(R.id.radio_fragment_content,homeFragment).commit();
break;
case R.id.rb_like:
if (likeFragment==null){
likeFragment=new LikeFragment();
}
transaction.replace(R.id.radio_fragment_content,likeFragment).commit();
break;
case R.id.rb_me:
if (meFragment==null){
meFragment=new MeFragment();
}
transaction.replace(R.id.radio_fragment_content,meFragment).commit();
break;
}
}
}
radiobutton的style文件
style文件
<style name="radiobutton_style">
<item name="android:layout_width">0dp</item>
<item name="android:padding">3dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:textSize">12sp</item>
</style>
每個按鈕的drawable是一個selector,如圖:

image.png
然后對于相應(yīng)的三個fragment,也是是分簡單,給出一個示例,具體剩下的就自己去添加了:
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.sub_fragment_home,container,false);
return view;
}
}