1、MainActivity布局文件
<?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"
android:orientation="vertical"
tools:context="com.example.radiobutton_fragment_navdemo.MainActivity">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"></FrameLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tab_one"
style="@style/MTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:drawableTop="@drawable/selector_main_btn_home"
android:text="首页"
android:textColor="@drawable/selector_main_text_color" />
<RadioButton
android:id="@+id/tab_two"
style="@style/MTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/selector_main_btn_hangqing"
android:text="行情"
android:textColor="@drawable/selector_main_text_color" />
<RadioButton
android:id="@+id/tab_three"
style="@style/MTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/selector_main_btn_account"
android:text="账户"
android:textColor="@drawable/selector_main_text_color" />
<RadioButton
android:id="@+id/tab_four"
style="@style/MTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/selector_main_btn_person"
android:text="个人"
android:textColor="@drawable/selector_main_text_color" />
</RadioGroup>
</LinearLayout>
2、styles样式
<style name="MTabStyle">
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">14sp</item>
<item name="android:minHeight">48dp</item>
<item name="android:drawablePadding">1dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:background">@drawable/btn_main_tab_selector</item>
</style>
3、colors文件
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="main_tab_bg">#545052</color>
<color name="colorWhite">#fff</color>
4、btn_main_tab_selector.xml 底部导航背景颜色 选中和未选中变化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/colorAccent" />
<item android:state_selected="true" android:drawable="@color/colorAccent" />
<item android:drawable="@color/main_tab_bg" />
</selector>
5、selector_main_text_color.xml 底部导航字体颜色 选中和未选中变化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/colorPrimary"/>
<!-- not selected -->
<item android:state_checked="false" android:color="@color/colorWhite"/>
</selector>
6、selector_main_btn_home.xml 底部导航图标选中和未选中图标变化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/tab_home_s" ></item>
<item android:state_checked="false" android:drawable="@drawable/tab_home_n"></item>
</selector>
7、MainActivity代码
private Fragment fragment;
private RadioGroup radioGroup;
private FragmentManager manager;
private Fragment_one fragment_one;
private Fragment_two fragment_two;
private Fragment_three fragment_three;
private Fragment_four fragment_four;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = getSupportFragmentManager().findFragmentById(R.id.fragment);
radioGroup = findViewById(R.id.radioGroup);
// 点击切换fragment
setFragment();
}
private void setFragment() {
manager = getSupportFragmentManager();
//创建事务
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragment_one = new Fragment_one();
fragment_two = new Fragment_two();
fragment_three = new Fragment_three();
fragment_four = new Fragment_four();
//添加事务
fragmentTransaction.add(R.id.fragment, fragment_one,"fragment_one");
fragmentTransaction.add(R.id.fragment, fragment_two,"fragment_two");
fragmentTransaction.add(R.id.fragment, fragment_three,"fragment_three");
fragmentTransaction.add(R.id.fragment, fragment_four,"fragment_four");
// 提交事务
fragmentTransaction.commit();
//RadioGroup点击事件监听
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.tab_one:{
Switchfragment("fragment_one");
}
break;
case R.id.tab_two:{
Switchfragment("fragment_two");
}
break;
case R.id.tab_three:{
Switchfragment("fragment_three");
}
break;
case R.id.tab_four:{
Switchfragment("fragment_four");
}
break;
}
}
});
// 默认显示第一个
Switchfragment("fragment_one");
}
protected void Switchfragment(String tag) {
// 事物
FragmentTransaction transaction = manager.beginTransaction();
if ("fragment_one".endsWith(tag)) {
transaction.show(fragment_one);
transaction.hide(fragment_two);
transaction.hide(fragment_three);
transaction.hide(fragment_four);
} else if ("fragment_two".endsWith(tag)) {
transaction.show(fragment_two);
transaction.hide(fragment_one);
transaction.hide(fragment_three);
transaction.hide(fragment_four);
} else if ("fragment_three".endsWith(tag)) {
transaction.show(fragment_three);
transaction.hide(fragment_one);
transaction.hide(fragment_two);
transaction.hide(fragment_four);
} else if ("fragment_four".endsWith(tag)) {
transaction.show(fragment_four);
transaction.hide(fragment_one);
transaction.hide(fragment_two);
transaction.hide(fragment_three);
}
transaction.commit();
}
8、创建Fragment
public class Fragment_one extends Fragment {
public static Fragment newInstance(){
Fragment_one fragment_one = new Fragment_one();
return fragment_one;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}