自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android底部導(dǎo)航欄實(shí)現(xiàn)(二)之RadioGroup

移動(dòng)開(kāi)發(fā) Android
這里簡(jiǎn)單記錄一下Android底部導(dǎo)航欄通過(guò)RadioGroup+Fragment的實(shí)現(xiàn)。

這里簡(jiǎn)單記錄一下Android底部導(dǎo)航欄通過(guò)RadioGroup+Fragment的實(shí)現(xiàn)。 

 

 

 

布局:

  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使用了狀態(tài)選擇器

  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默認(rèn)帶的圓點(diǎn)--> 
  7.         <item name="android:gravity">center</item> 
  8.         <item name="android:textSize">12sp</item> 
  9.     </style>  

代碼

初始化的代碼就不記錄了,都是一些findViewById,實(shí)現(xiàn)過(guò)程無(wú)非就是對(duì)RadioButton進(jìn)行監(jiān)聽(tīng)一下:

  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();//設(shè)置狀態(tài) 
  34.         transaction.commit(); 
  35.     }  

狀態(tài)的設(shè)置 

  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();我寫(xiě)在onCreateVew里面并沒(méi)有生效。這里我寫(xiě)在了onStart()方法里了。

  1. @Override 
  2.    public void onStart() { 
  3.        setDefaultFragment();//寫(xiě)在onCreateView里面,當(dāng)頁(yè)面跑到其他Fragment再回來(lái)就不會(huì)生效 
  4.        super.onStart(); 
  5.    }  

說(shuō)明:這幾篇文章沒(méi)有過(guò)多的文字?jǐn)⑹?,因?yàn)檫@些東西也不是很難,而且都是常用的,相信很多人都了如指掌了,多說(shuō)亦是廢話,直接上代碼看的反而更清楚。

責(zé)任編輯:龐桂玉 來(lái)源: segmentfault
相關(guān)推薦

2016-12-07 10:18:44

移動(dòng)應(yīng)用開(kāi)發(fā)底部導(dǎo)航android

2016-12-07 10:58:35

移動(dòng)應(yīng)用開(kāi)發(fā)底部導(dǎo)航android

2016-12-07 10:32:14

移動(dòng)應(yīng)用開(kāi)發(fā)底部導(dǎo)航android

2016-12-07 10:02:54

移動(dòng)應(yīng)用開(kāi)發(fā)底部導(dǎo)航android

2022-08-08 19:46:26

ArkUI鴻蒙

2023-10-23 08:48:04

CSS寬度標(biāo)題

2021-08-02 09:11:39

底部導(dǎo)航產(chǎn)品Tab Bar

2022-11-15 18:31:37

React

2009-06-24 09:36:52

XML實(shí)現(xiàn)breadcMVC

2021-01-28 06:11:40

導(dǎo)航組件Sidenav Javascript

2020-11-23 14:29:22

Android 10窗口代碼

2017-02-17 11:00:57

狀態(tài)欄Android

2022-02-11 14:48:57

Chrome谷歌下載欄

2012-04-28 11:07:15

2023-06-06 15:38:28

HTMLCSS開(kāi)發(fā)

2013-01-14 17:05:55

UCUI設(shè)計(jì)菜單欄

2015-07-30 14:43:04

導(dǎo)航欄iOS開(kāi)發(fā)

2021-01-04 11:44:05

鴻蒙HarmonyOSAbilitySlic

2023-12-29 08:06:40

開(kāi)源軟件導(dǎo)航前端

2018-04-15 15:51:21

Android P安卓系統(tǒng)谷歌
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)