成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

2021-01-28 06:11:40

導航組件Sidenav Javascript

2009-06-24 09:36:52

XML實現breadcMVC

2020-11-23 14:29:22

Android 10窗口代碼

2017-02-17 11:00:57

狀態欄Android

2013-01-14 17:05:55

UCUI設計菜單欄

2022-02-11 14:48:57

Chrome谷歌下載欄

2023-06-06 15:38:28

HTMLCSS開發

2012-04-28 11:07:15

2015-07-30 14:43:04

導航欄iOS開發

2021-01-04 11:44:05

鴻蒙HarmonyOSAbilitySlic

2023-12-29 08:06:40

開源軟件導航前端

2024-08-29 08:13:58

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 黄色大片在线免费观看 | 蜜臀久久 | 国产精品揄拍一区二区久久国内亚洲精 | 精品久久一 | 精品久久久久久久久亚洲 | 国内自拍偷拍视频 | 亚洲精品av在线 | 久久成人av电影 | 久久国产精品免费 | aaaaaaa片毛片免费观看 | 国产一级淫片免费视频 | 欧美成视频在线观看 | 成人在线视频网址 | 亚洲一区 中文字幕 | 精品国产乱码久久久久久闺蜜 | 亚洲午夜精品在线观看 | 国产精品人人做人人爽 | 中文字幕高清在线 | 狠狠躁夜夜躁人人爽天天高潮 | 日韩欧美天堂 | 91人人澡人人爽 | 欧美性受xxxx白人性爽 | 一区二区三区免费 | 超碰在线播 | 日本精品视频在线观看 | 精品一区二区三区在线观看国产 | 日韩久久久久久久 | 九九热在线视频 | 国产精品国产三级国产aⅴ中文 | 色综合天天天天做夜夜夜夜做 | 久久精彩视频 | 中文在线一区二区 | 国产一区二区三区 | 亚洲精选久久 | 国产一区二区三区亚洲 | 精品成人av | 亚洲精品自在在线观看 | 久久久久久久久久久久91 | 国产精品永久免费 | 国产日韩久久 | 精品国产不卡一区二区三区 |