Android--RadioGroup和Tablayout两种实现底部导航的方式详解

        自从Android3.0引入Fargment之后,在Activity中使用底部导航进行Fragment的切换已经越来越普遍,或者可以说已经成为了移动应用的标配,而本篇文章我总结了项目中常用的几种实现导航的方式,分别是RadioGroup、Tablayout、RadioGroup+反射和FragmentTabHost四种实现方式,包含底部和顶部的双导航界面的实现,实现的结果类似下图所示:

   


      Github下载地址:https://github.com/huohaoliz/BottomNavigation

一、使用RadioGroup+Fragment实现底部导航,使用TabLayout+Fragment实现顶部导航

        1,RadioGroup+Fragment的形式是之前开发中比较受欢迎,使用比较多的一种实现形式,所以把它排到第一位。好了不扯了,直接代码走起:

       首先,在radiogroup.xml中的布局文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.linktestproject.RadioGroupActivity">

    <FrameLayout
        android:id="@+id/fl_radio_show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></FrameLayout>
    <RadioGroup
        android:id="@+id/rg_radio_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom">
        <RadioButton
            android:id="@+id/rb_radio_homepage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/homepage"
            android:text="@string/homepage"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_radio_subscription"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/subscription"
            android:text="@string/subscription"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_radio_find"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/find"
            android:text="@string/find"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_radio_mine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/mine"
            android:text="@string/mine"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
    </RadioGroup>
</LinearLayout>

        我们需要对每个RadioButton的图片资源做一个选择器,在drawable文件夹下添加四个选择器,内容是(这是首页的图片选择器,其他与此类似就不贴代码了):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/tab4_down"></item>
    <item android:state_checked="false" android:drawable="@mipmap/tab4"></item>
</selector>
之后我们还要对选中的RadioButton添加字体颜色的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/textChecked"></item>
    <item android:state_checked="false" android:color="@color/textUnChecked"></item>
</selector>
  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值