怎么在RadioButton中使用片段实现一个底部导航栏效果

  介绍

怎么在RadioButton中使用片段实现一个底部导航栏效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

首先我们打开RadioButtonDemo这个项目,首先修改activity_main。xml文件如下:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"   ,xmlns:工具=癶ttp://schemas.android.com/tools"   ,android: layout_width=癿atch_parent"   ,android: layout_height=癿atch_parent"   ,android:取向=皏ertical"   ,工具:上下文=癱om.example.jackhu.radiobuttondemo.MainActivity"祝辞      & lt; FrameLayout   android:才能id=癅 + id/mFragment"   android:才能layout_width=癿atch_parent"   android:才能layout_height=? dp"   android:才能layout_weight=?“祝辞& lt;/FrameLayout>      & lt; RadioGroup   android:才能layout_marginBottom=? dp"   android:才能id=癅 + id/mRadioGroup"   android:才能取向=癶orizontal"   android:才能layout_width=癿atch_parent"   android:才能layout_height=?8 dp"比;   & lt; RadioButton才能   ,,android: drawableTop=癅drawable/rbhome"   ,,android:按钮=癅null"   ,,android:检查=皌rue"   ,,android:输入textColor=癅color/colorRadioButtonP"   ,,android: id=癅 + id/mRb_home"   ,,android:重力=癱enter"   ,,android: layout_width=? dp"   ,,android:文本=癏ome"   ,,android: layout_weight=?”;   ,,android: layout_height=癿atch_parent",/比;      & lt; RadioButton才能   ,,android: drawableTop=癅drawable/rb_message"   ,,android:按钮=癅null"   ,,android:输入textColor=癅color/colorRadioButtonN"   ,,android: id=癅 + id/mRb_message"   ,,android:重力=癱enter"   ,,android: layout_width=? dp"   ,,android:文本=癕essage"   ,,android: layout_weight=?”;   ,,android: layout_height=癿atch_parent",/比;      & lt; RadioButton才能   ,,android: drawableTop=癅drawable/rbfind"   ,,android:按钮=癅null"   ,,android:输入textColor=癅color/colorRadioButtonN"   ,,android: id=癅 + id/mRb_find"   ,,android:重力=癱enter"   ,,android: layout_width=? dp"   ,,android:文本=癋ind"   ,,android: layout_weight=?”;   ,,android: layout_height=癿atch_parent",/比;      & lt; RadioButton才能   ,,android: drawableTop=癅drawable/rbmy"   ,,android:按钮=癅null"   ,,android:输入textColor=癅color/colorRadioButtonN"   ,,android: id=癅 + id/mRb_my"   ,,android:重力=癱enter"   ,,android: layout_width=? dp"   ,,android:文本=癕y"   ,,android: layout_weight=?”;   ,,android: layout_height=癿atch_parent",/比;      ,& lt;/RadioGroup>      & lt;/LinearLayout>

这里我们在布局文件片段控件:用于显示界面的切换。

RadioGroup控件包含了4个RadioButton:用于显示按钮。我们给第一个按钮检查为真正的默认选中。其中android:按钮=癅null”取消圆点。

drawableTop属性:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; selector  xmlns: android=癶ttp://schemas.android.com/apk/res/android"比;   ,& lt; item  android: state_checked=皌rue", android:可拉的=癅drawable/home_p"/比;   ,& lt; item  android:可拉的=癅drawable/home_n"/比;   & lt;/selector>

显示选择和未选中的状态的图标

创建片段,加载片段布局文件,类代码如下:

package  com.example.jackhu.radiobuttondemo.fragment;         import  android.os.Bundle;   import  android.support.v4.app.Fragment;   import  android.view.LayoutInflater;   import  android.view.View;   import  android.view.ViewGroup;      import  com.example.jackhu.radiobuttondemo.R;/* *   ,* A  simple  {@link 片段}子类。   ,*/public  class  HomeFragment  extends  Fragment  {         ,public  HomeFragment (), {//才能,Required  empty  public 构造函数   ,}      ,//单例模式   ,public  static  HomeFragment  newInstance () {   HomeFragment 才能homeFragment=new  homeFragment ();   return 才能;homeFragment;   ,}      ,@Override   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   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   null   null   null   null

怎么在RadioButton中使用片段实现一个底部导航栏效果