Android使用RadioGroup实现底部导航栏

  

RadioGroup实现底部导航栏效果,如图::

  

,  Android使用RadioGroup实现底部导航栏”> </p>
  <p>实现可最基本的导航栏功能,不能左右滑动,只能点击</p>
  <p> 1。内嵌的片段的布局:</p>
  
  <pre类=   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android:取向=按怪薄盿ndroid: layout_width=癿atch_parent”   android: layout_height=癿atch_parent”比;   & lt; TextView   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:重力="中心"   android: textSize=" 50 sp”   android:输入textColor=" @color/colorPrimary”   android:文本=凹摇?比;   & lt;/LinearLayout>   之前      

2.片段的活动代码:

        公开课FrHome延伸片段{      @Nullable   @Override   公众视野>   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;项目android: state_checked=" true " android:颜色=" # 3 f51b5 "/比;   & lt;项目android: state_checked=" false " android:颜色=" # 8 f8f8f "/比;   & lt;/selector>   之前      

点击改变图标:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;项目android: state_checked=" true " android:可拉的=癅mipmap/ic_history_checked”/比;   & lt;项目android: state_checked=" false " android:可拉的=癅mipmap/ic_history_unchecked”/比;   & lt;/selector>   之前      

界面布局:

  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   xmlns:应用=" http://schemas.android.com/apk/res-auto "   工具:上下文=癱om.lotus.chartspagedemo.ActHome”比;      & lt; FrameLayout   android: id=癅 + id/frame_layout”   android: layout_width=" match_parent "   android: layout_above=癅 + id/card_view”   android: layout_height=" match_parent "/比;      & lt; android.support.v7.widget.CardView   android: id=癅 + id/card_view”   应用:cardElevation=" 25 dp "   android: layout_alignParentBottom=" true "   android: layout_width=" match_parent "   android: layout_height=皐rap_content”比;   & lt; RadioGroup   android: paddingTop=" 5 dp "   android: id=癅 + id/tab_bar”   android:背景=" @color/app_white”   android: layout_width=" match_parent "   android: layout_height=" 60 dp "   android:重力="中心"   面向android:="水平"比;   & lt; RadioButton   android: id=癅 + id/tab_home”   android:重力="中心"   android:按钮=" @null "   android: drawableTop=" @drawable/selector_tab_home”   android: layout_width=" 0 dp”   android: layout_weight=" 1 "   android: layout_height=" match_parent "   android:输入textColor=" @drawable/selector_tab_color”   android:文本="首页”/比;   & lt; RadioButton   android: id=癅 + id/tab_health”   android:重力="中心"   android:按钮=" @null "   android: drawableTop=" @drawable/selector_tab_health”   android: layout_width=" 0 dp”   android: layout_weight=" 1 "   android: layout_height=" match_parent "   android:输入textColor=" @drawable/selector_tab_color”   android:文本="体检测评”/比;   & lt; RadioButton   android: id=癅 + id/tab_personal”   android:重力="中心"   android:按钮=" @null "   android: drawableTop=" @drawable/selector_tab_personal”   android: layout_width=" 0 dp”   android: layout_weight=" 1 "   android: layout_height=" match_parent "   android:输入textColor=" @drawable/selector_tab_color”   android:文本="个人中心”/比;   & lt;/RadioGroup>   & lt;/android.support.v7.widget.CardView>      & lt;/RelativeLayout>   之前      

4。装载片段的界面活动的代码(加入双击返回键则退出应用):

        公共类ActHome FragmentActivity实现RadioGroup延伸。OnCheckedChangeListener {      @BindView (R.id.frame_layout)   FrameLayout FrameLayout;   @BindView (R.id.tab_home)   RadioButton tabHome;   @BindView (R.id.tab_health)   RadioButton tabHealth;   @BindView (R.id.tab_personal)   RadioButton tabPersonal;   @BindView (R.id.tab_bar)   RadioGroup tabBar;      最后公共静态字符串ACTION_EXIT_SYSTEM=皊ys_exit”;      私人FragmentManager经理;   私人FragmentTransaction事务;   私人FrHome FrHome;   私人FrHealth FrHealth;   私人FrPersonal FrPersonal;   私人长mExitTime;      @Override   保护无效onCreate(包savedInstanceState) {   super.onCreate (savedInstanceState);   setContentView (R.layout.activity_home);   ButterKnife.bind(这个);   RadioButton tabHome=(RadioButton) tabBar.getChildAt (0);   tabHome.setChecked(真正的);   tabBar.setOnCheckedChangeListener(这个);   initFragment ();   }      私人空间initFragment () {   经理=getSupportFragmentManager ();   事务=manager.beginTransaction ();   frHome=new frHome ();   transaction.add (R.id.frame_layout frHome);   transaction.commit ();   }      @Override   公共空间onCheckedChanged (RadioGroup RadioGroup, @IdRes int checkedId) {   开关(checkedId) {   案例R.id.tab_home:   FragmentTransaction ft1=manager.beginTransaction ();   hideAll (ft1);   如果(frHome !=null) {   ft1.show (frHome);   其他}{   frHome=new frHome ();   ft1.add (R.id.frame_layout frHome);   }   ft1.commit ();   打破;   案例R.id.tab_health:   FragmentTransaction ft2=manager.beginTransaction ();   hideAll (ft2);   如果(frHealth !=null) {   ft2.show (frHealth);   其他}{   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android使用RadioGroup实现底部导航栏