简单效果:
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进行监听
return view;
}
@Override
public void onStart() {
setDefaultFragment();//写在onCreateView里面,当页面跑到其他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,如图:
然后对于相应的三个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;
}
}