使用radiobutton与fragment实现底部导航栏

最近在学习android,在开发中需要实现一个底部导航栏功能,与微信底部导航类似。

效果图如下:

 

网上查看了很多资料,并没有发现相关控件(可能本人是个菜鸟级别的)。

于是选用了网上较流行的一种方式实现。那就是采用radiobutton+fragment的方式实现。

原理很简单:在窗口底部放上一排radiobutton,通过StateListDrawable设置按钮状态变化的显示效果,通过监控点击按钮事件,为窗口添加fragment。

以下是主要的实现步骤:

1.窗口布局

外层采用RelativeLayout布局,内层放入一个FrameLayout与一个LinearLayout布局,LinearLayout放置在窗口底部,在其内部放入radiogroup及radiobutton

 

FrameLayout 配置代码

    <FrameLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_above="@+id/linearLayout"
        android:id="@+id/main_fragment">

    </FrameLayout>

 LinearLayout布局代码

<LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linearLayout">

 radiogroup及radiobutton部分代码

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton//该配置来源于网上,直接使用即可
                android:id="@+id/bt_bottom_near"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/near_bt_selector"//设置背景图片变换效果
                android:gravity="center_horizontal|bottom"
                android:paddingTop="2dp"
                android:text="@string/bt_bottom_near"
                android:textColor="@drawable/bottom_bt_text_color" />//设置字体变换效果
  ......

 near_bt_selector.xml 代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/near_active">//选中状态显示效果
    </item>
    <item android:state_checked="false"
        android:drawable="@drawable/near_black"></item>//未被选中样式效果
</selector>

 bottom_bt_text_color.xml代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="@android:color/holo_orange_dark"/>//选中颜色
    <item android:state_checked="false" android:color="@android:color/black"/>//未被选中颜色
    <item android:color="@android:color/black"/>//初始颜色

</selector>

 到这里,xml的配置都已经做好了。

2.实现打开fragment,fragment之间切换,定义按钮的click事件

主要代码如下,主要在click事件里打开一个fragment。

@Override
            public void onClick(View v) {
                MsgFragment msgFragment = new MsgFragment();//新建fragment实例
                Bundle args = new Bundle();//绑定参数
                args.putString("args", getResources().getString(R.string.bt_bottom_msg));
                msgFragment.setArguments(args);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, msgFragment).commit();//记得提交
            }

 3.fragment类的实现,需要继承Fragment基类(需要注意版本问题)

主要代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);//获得视图
        tvMsgShow = (TextView) view.findViewById(R.id.msgShow);
        tvMsgShow.setText(getArguments().getString("args"));//显示参数值
        return view;
    }

 OK,这里主要的功能都已实现。代码只提供了部分,但是相信大家还是能根据提示完成自己的功能。

 

 

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android底部导航栏实现可以通过使用RadioGroup和Fragment结合使用实现。首先,在布局文件中添加一个RadioGroup,然后在其中添加多个RadioButton,每个RadioButton代表一个导航项。接着,使用Fragment实现每个导航项对应的页面内容。在RadioGroup中设置监听器,当用户点击某个RadioButton时,切换到对应的Fragment页面。这样就可以实现一个简单的底部导航栏了。 ### 回答2: Android底部导航栏实现一般可以通过RadioGroup和Fragment结合使用来完成。首先,在XML布局文件中定义一个RadioGroup,并在其中添加多个RadioButton,每个RadioButton对应导航栏中的一个选项。 接下来,在Activity中,我们需要定义一个Fragment的容器,用于加载不同的Fragment页面。在RadioGroup的选择监听器中,根据所选中的RadioButton的id,切换对应的Fragment页面。 在切换Fragment页面时,可以使用FragmentManager和FragmentTransaction来实现。通过FragmentManager的beginTransaction()方法获取一个FragmentTransaction的实例,然后使用replace()方法将容器中的Fragment替换为选中的目标Fragment,最后通过commit()方法提交事务即可完成页面切换。 为了方便管理和切换Fragment,可以定义一个Fragment的集合,用于存放所有的Fragment实例,并在选择监听器中根据RadioButton的顺序获取对应的Fragment。 另外,为了保持Fragment的状态,在切换Fragment时可以使用hide()和show()方法而不是replace()来隐藏和显示Fragment,这样可以避免Fragment被重复实例化。 总结来说,通过RadioGroup和Fragment的结合使用,我们可以实现Android底部导航栏的功能。通过监听RadioGroup中RadioButton的选择事件,来切换不同的Fragment页面。这样可以实现底部导航栏的选项切换,同时保持Fragment的状态。 ### 回答3: Android底部导航栏实现可以通过使用RadioGroup和Fragment实现。 首先,我们可以在布局文件中创建一个包含多个RadioButton的RadioGroup,这些RadioButton将作为底部导航栏的按钮。我们可以为每个RadioButton设置图标和文本,以表示不同的导航选项。通过设置RadioGroup的布局属性可以将其放置在屏幕的底部。 接下来,我们需要创建对应的Fragment,并在Activity中使用FragmentTransaction来进行Fragment的切换。在FragmentTransaction中,我们可以通过调用replace方法来替换Activity中的显示内容,将选中的RadioButton对应的Fragment显示出来。 当用户点击底部导航栏RadioButton时,我们可以通过设置RadioGroup的OnCheckedChangeListener来监听选中项的变化。当选中项发生变化时,我们可以获取选中的RadioButton对应的Fragment,并使用FragmentTransaction来进行Fragment的切换,从而显示选中的Fragment。 此外,为了更好地控制底部导航栏的切换效果,我们还可以使用fragment的缓存机制以提高切换的效率,避免每次切换都重新创建Fragment对象。 总之,通过结合RadioGroup和Fragment使用,我们可以方便地实现Android底部导航栏的功能,使用户可以方便地在不同的导航选项之间进行切换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值