当前位置:主页 > 查看内容

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

发布时间:2021-09-19 00:00| 位朋友查看

简介:这里简单记录一下Android底部导航栏通过RadioGroup+Fragment的实现。 布局: ?xmlversion= 1.0 encoding= utf-8 ? RelativeLayoutxmlns:android= http://schemas.android.com/apk/res/android android:layout_width= match_parent android:layout_height= ma……

这里简单记录一下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.    }  

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


本文转载自网络,原文链接:https://segmentfault.com/a/1190000007698520
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐