Android底部导航栏实现(二)之RadioGroup

移动开发 Android
这里简单记录一下Android底部导航栏通过RadioGroup+Fragment的实现。

这里简单记录一下Android底部导航栏通过RadioGroup+Fragment的实现。 

 

 

 

布局:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.                 android:layout_width="match_parent" 
  4.                 android:layout_height="match_parent" 
  5.                 android:orientation="vertical"
  6.  
  7.     <include layout="@layout/fragment_content"/> 
  8.  
  9.     <View 
  10.         android:layout_width="match_parent" 
  11.         android:layout_height="1px" 
  12.         android:background="@color/grey_300" 
  13.         android:elevation="20dp"/> 
  14.  
  15.     <RadioGroup 
  16.         android:id="@+id/radio_group" 
  17.         android:layout_width="match_parent" 
  18.         android:layout_height="56dp" 
  19.         android:layout_alignParentBottom="true" 
  20.         android:background="@color/white" 
  21.         android:orientation="horizontal"
  22.  
  23.         <RadioButton 
  24.             android:id="@+id/rb_home" 
  25.             style="@style/radiobutton_style" 
  26.             android:checked="true" 
  27.             android:drawableTop="@drawable/radiobutton_bg_home" 
  28.             android:text="@string/item_home" 
  29.             /> 
  30.  
  31.         <RadioButton 
  32.             android:id="@+id/rb_location" 
  33.             style="@style/radiobutton_style" 
  34.             android:drawableTop="@drawable/radiobutton_bg_location" 
  35.             android:text="@string/item_location"/> 
  36.  
  37.         <RadioButton 
  38.             android:id="@+id/rb_like" 
  39.             style="@style/radiobutton_style" 
  40.             android:drawableTop="@drawable/radiobutton_bg_like" 
  41.             android:text="@string/item_like"/> 
  42.  
  43.         <RadioButton 
  44.             android:id="@+id/rb_me" 
  45.             style="@style/radiobutton_style" 
  46.             android:drawableTop="@drawable/radiobutton_bg_me" 
  47.             android:text="@string/item_person"/> 
  48.  
  49.     </RadioGroup> 
  50. </RelativeLayout>  

这里的drawableTop使用了状态选择器

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android"
  2.     <item android:drawable="@drawable/home_fill" android:state_checked="true"/> 
  3.     <item android:drawable="@drawable/home"/> 
  4. </selector>  

style

  1. <style name="radiobutton_style"
  2.         <item name="android:layout_width">0dp</item> 
  3.         <item name="android:padding">3dp</item> 
  4.         <item name="android:layout_height">match_parent</item> 
  5.         <item name="android:layout_weight">1</item> 
  6.         <item name="android:button">@null</item><!--去除RadioButton默认带的圆点--> 
  7.         <item name="android:gravity">center</item> 
  8.         <item name="android:textSize">12sp</item> 
  9.     </style>  

代码

初始化的代码就不记录了,都是一些findViewById,实现过程无非就是对RadioButton进行监听一下:

  1. mRadioGroup.setOnCheckedChangeListener(this); 
  2.  
  3.  
  4.     @Override 
  5.     public void onCheckedChanged(RadioGroup groupint checkId) { 
  6.         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
  7.         switch (checkId) { 
  8.             case R.id.rb_home: 
  9.                 if (mHomeFragment == null) { 
  10.                     mHomeFragment = HomeFragment.newInstance(getString(R.string.item_home)); 
  11.                 } 
  12.                 transaction.replace(R.id.sub_content, mHomeFragment); 
  13.                 break; 
  14.             case R.id.rb_location: 
  15.                 if (mLocationFragment == null) { 
  16.                     mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location)); 
  17.                 } 
  18.                 transaction.replace(R.id.sub_content, mLocationFragment); 
  19.                 break; 
  20.             case R.id.rb_like: 
  21.                 if (mLikeFragment == null) { 
  22.                     mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like)); 
  23.                 } 
  24.                 transaction.replace(R.id.sub_content, mLikeFragment); 
  25.                 break; 
  26.             case R.id.rb_me: 
  27.                 if (mPersonFragment == null) { 
  28.                     mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person)); 
  29.                 } 
  30.                 transaction.replace(R.id.sub_content, mPersonFragment); 
  31.                 break; 
  32.         } 
  33.         setTabState();//设置状态 
  34.         transaction.commit(); 
  35.     }  

状态的设置 

  1. private void setTabState() { 
  2.         setHomeState(); 
  3.         setLocationState(); 
  4.         setLikeState(); 
  5.         setMeState(); 
  6.  
  7.     } 
  8.  
  9.     /** 
  10.      * set tab home state 
  11.      */ 
  12.     private void setHomeState() { 
  13.         if (mRadioHome.isChecked()) { 
  14.             mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); 
  15.         } else { 
  16.             mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); 
  17.         } 
  18.     } 
  19.  
  20.     private void setLocationState() { 
  21.         if (mRadioLocation.isChecked()) { 
  22.             mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); 
  23.         } else { 
  24.             mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); 
  25.         } 
  26.     } 
  27.  
  28.     private void setLikeState() { 
  29.         if (mRadioLike.isChecked()) { 
  30.             mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); 
  31.         } else { 
  32.             mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); 
  33.         } 
  34.     } 
  35.  
  36.     private void setMeState() { 
  37.         if (mRadioMe.isChecked()) { 
  38.             mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); 
  39.         } else { 
  40.             mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); 
  41.         } 
  42.     }  

这里需要注意的是, setDefaultFragment();我写在onCreateVew里面并没有生效。这里我写在了onStart()方法里了。

  1. @Override 
  2.    public void onStart() { 
  3.        setDefaultFragment();//写在onCreateView里面,当页面跑到其他Fragment再回来就不会生效 
  4.        super.onStart(); 
  5.    }  

说明:这几篇文章没有过多的文字叙述,因为这些东西也不是很难,而且都是常用的,相信很多人都了如指掌了,多说亦是废话,直接上代码看的反而更清楚。

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2016-12-07 10:18:44

移动应用开发底部导航android

2016-12-07 10:58:35

移动应用开发底部导航android

2016-12-07 10:32:14

移动应用开发底部导航android

2016-12-07 10:02:54

移动应用开发底部导航android

2022-08-08 19:46:26

ArkUI鸿蒙

2023-10-23 08:48:04

CSS宽度标题

2022-11-15 18:31:37

React

2021-08-02 09:11:39

底部导航产品Tab Bar

2009-06-24 09:36:52

XML实现breadcMVC

2021-01-28 06:11:40

导航组件Sidenav Javascript

2020-11-23 14:29:22

Android 10窗口代码

2013-01-14 17:05:55

UCUI设计菜单栏

2017-02-17 11:00:57

状态栏Android

2023-06-06 15:38:28

HTMLCSS开发

2022-02-11 14:48:57

Chrome谷歌下载栏

2015-07-30 14:43:04

导航栏iOS开发

2012-04-28 11:07:15

2018-04-15 15:51:21

Android P安卓系统谷歌

2010-02-05 16:21:02

Android导航

2017-08-10 16:36:43

Android导航标签
点赞
收藏

51CTO技术栈公众号