RadioButton+Fragment实现Tab切换

安卓初学者:framelayout实现简单的tab导航栏 主要运用radiogroup和framelayout结合实习简单的tab切换;首先在drawable下创建selector_radiogroup.xml和shape_radiogroup.xml<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk... 阅读详情

第一次写博客 激动啊 - -!
话不多说了,直接上代码:

HomeActivity代码:

public class HomeActivity extends FragmentActivity{
    private FragmentTransaction transaction;
    private RadioGroup radioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        /**
         * 设置每个RadioButton的监听事件,然后切换对应的Fragment
         */
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //开启事务
                transaction = getSupportFragmentManager().beginTransaction();
                switch (i) {
                    case R.id.weixin:
                        transaction.replace(R.id.content, new WeixinFragment());
                        break;
                    case R.id.tongxunlu:
                        transaction.replace(R.id.content, new TongxunluFragment());
                        break;
                    case R.id.friendcircle:
                        transaction.replace(R.id.content, new FriendFragment());
                        break;
                    case R.id.setting:
                        transaction.replace(R.id.content, new SettingFragment());
                        break;
                }
                //事务提交
                transaction.commit();
            }
        });
    }

    /**
     * 一进入主页的时候,自动加载第一页tab
     */
    @Override
    protected void onResume() {
        super.onResume();
        weixin.setChecked(true);
    }
}

接下来是四个Tab选项(在这就写一个,剩下3个大同小异)

public class WeixinFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_weixin,container,false);
    }
}

首页xml(顶部和底部事先写好 ,然后直接include即可,中部是一个FrameLayout占位,然后transaction.replace()替换即可)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".HomeActivity">
    <include layout="@layout/top"></include>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#000000"
        android:layout_marginBottom="5dp"/>
    <include layout="@layout/bottom"></include>
</LinearLayout>

这是底部的RadioGroup

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="18sp"
            android:textColor="@color/ziti_color"
            android:drawableTop="@drawable/weixin_selctor"
            android:gravity="center_horizontal"
            android:button="@null"
            android:text="微信"/>
        <RadioButton
            android:id="@+id/tongxunlu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="18sp"
            android:textColor="@color/ziti_color"
            android:drawableTop="@drawable/address_selctor"
            android:gravity="center_horizontal"
            android:button="@null"
            android:text="通讯录"/>
        <RadioButton
            android:id="@+id/friendcircle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="18sp"
            android:textColor="@color/ziti_color"
            android:drawableTop="@drawable/find_selctor"
            android:gravity="center_horizontal"
            android:button="@null"
            android:text="朋友圈"/>
        <RadioButton
            android:id="@+id/setting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="18sp"
            android:textColor="@color/ziti_color"
            android:drawableTop="@drawable/setting_selctor"
            android:gravity="center_horizontal"
            android:button="@null"
            android:text="设置"/>
    </RadioGroup>
</LinearLayout>

PS:RadioButton图片跟字体颜色需单独设置selector,代码如下

图片selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/tab_weixin_pressed"></item>
    <item android:drawable="@drawable/tab_weixin_normal"></item>
</selector>

字体颜色selector(在res资源目录里创建一个color的文件夹,然后里面创建字体颜色的selector即可)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00ff00" android:state_checked="true"></item>
    <item android:color="#969696"></item>
</selector>

—————————————————————————————————————————————————————
有时遇到需要底部中间的那个Tab凸出的效果
在布局的根节点需添加:

android:clipChildren="false"

所有Tab的父布局定高,设置

android:gravity="bottom"

每个Tab也定高(需要凸出的那个Tab高度设置的高一点),设置:

android:gravity="bottom|center_horizontal"

最后代码控制ImageView图片的切换和文字颜色的改变即可,核心代码:

	@OnClick({R.id.ll_home, R.id.ll_my})
    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                setTab(0);
                break;
            case R.id.ll_my:
                setTab(1);
                break;
        }
    }

    private void setTab(int index) {
        //先都恢复到未选中状态
        iv_home.setImageResource(R.drawable.icon_home_unselected);
        tv_home.setTextColor(Color.parseColor("#cccccc"));
        iv_my.setImageResource(R.drawable.icon_my_unselected);
        tv_my.setTextColor(Color.parseColor("#cccccc"));
        switch (index) {
            case 0:
                iv_home.setImageResource(R.drawable.icon_home_selected);
                tv_home.setTextColor(Color.parseColor("#1296DB"));
                break;
            case 1:
                iv_my.setImageResource(R.drawable.icon_my_selected);
                tv_my.setTextColor(Color.parseColor("#1296DB"));
                break;
        }
    }

效果图如下:
在这里插入图片描述

android框架搭建radiogroup+fragment Android最基本的框架搭建,radio group+fragment,上面是fragement,下面是button按钮,实现切换 立即下载

相关推荐

RadioGroup实现APP首页底部Tab的切换

RadioGroup实现APP首页底部Tab的切换,原文链接:http://www.cnblogs.com/smyhvae/p/4463931.html

工具类——Fragment+radiortoup切换

/** * 主界面 底部切换tab工具类 */ public class FragmentTabUtils implements RadioGroup.OnCheckedChangeListener { private List&lt;Fragment&gt; fragments; // 一个tab页面对应一个Fragment private RadioGroup rgs; /...

九龙冰哥 169

用RadioGroup实现fragment的界面切换

用RadioGroup实现fragment的界面切换

kotlin 切换 fragment

Kotlin 切换 frragment 的技巧

lujianfeiccie2009的专栏 1240

MacOS新手必看:HomeBrew安装避坑指南(含M1芯片适配方案)

本文为MacOS新手提供全面的HomeBrew安装指南,特别包含M1/M2芯片适配方案。从系统准备到核心安装流程,再到日常维护和高级配置,详细解析每个步骤的潜在问题及解决方案,帮助用户高效管理Mac软件。

weixin_28831399的博客 471

网络可视化工具netron详细安装流程

netron安装目录1.netron 简介2.不同操作系统下的安装使用方式2.1 在线版本:只要有浏览器就可以2.2 不同操作系统安装使用3.netron支持的网络框架4.netron测试演示 1.netron 简介 在实际的项目中,经过会遇到各种网络模型,需要我们快速去了解网络结构。如果单纯的去看模型文件,脑海中很难直观的浮现网络的架构。 这时,就可以使用netron可视化工具,可以清晰的看到每一层的输入输出,网络总体的架构,而且支持各种不同网络框架,简单好用。 2.不同操作系统下的安装使用方式 2.1

江南研习社 12万+

Kotlin项目实战之手机影音---主界面tab切换、home界面适配、获得首页网络数据

主界面tab切换: 添加点击事件: 接下来需要处理一下主界面TAB的切换了,这里先添加BottomBar的监听事件: class MainActivity : BaseActivity(), ToolBarManager { override fun getLayoutId(): Int { return R.layout.activity_main } ...

webor2006的博客 798

【UE5+Quest3】手部模型替换---3-0

这次手部模型替换遇到了一些小问题,不过好在是bug一个个都处理掉了,我会分开几个章节去整理。详细的视频教程可以参考油管大佬的视频手部素材放到文章末尾,自行下载使用。(仅用作学习研究,不可用于商用)

qbbmnnnnnn的博客 944

RadioButton不能切换问题

在xml里已经设置RadioGroup下的某一个RadioButton   checked="true" ,那么这个已经设置为选中的RadioButton  是不参与RadioGroup下其他RadioButton 的切换,会一直是选中状态,如果想设置为选中又要参与RadioGroup下切换,可以使用代码设置。

sky_castle的专栏 1175

十三、FLUENT蒸发/冷凝模型实战:从沸腾模拟到常温蒸发的UDF进阶

本文深入探讨FLUENT蒸发/冷凝模型的应用实践,从沸腾模拟到常温蒸发的UDF进阶。通过分析相变模型的核心逻辑、关键陷阱及UDF动态饱和温度场的实现,帮助工程师准确模拟不同场景下的蒸发/冷凝过程。结合太阳能蒸馏器等工程案例,提供优化方案和常见问题解决方案,显著提升模拟精度与效率。

weixin_42548893的博客 571

STM32L476低功耗实战:LPTIM精准定时唤醒STOP2模式与系统级功耗优化

本文深入探讨了STM32L476低功耗实战技巧,重点解析了LPTIM精准定时唤醒STOP2模式的配置与优化。通过详细的步骤指南和实战经验分享,帮助开发者实现系统级功耗优化,特别适合电池供电设备开发。文章涵盖LPTIM1定时器配置、STOP2模式进入流程及常见问题排查,助力将整机功耗降至8.2uA级别。

weixin_33686714的博客 435

Fragment+RadioButton实现Tab页切换

总所周知,实现一个App的Tab页切换的方式有很多种。例如使用ViewPager+Fragment,RadioButton+Fragment。今天主要来利用RadioButton来实现。 首先我们绘制一个布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ap

WJ_Dawson的博客 874

数据安全实战技能解析:从加密攻防到数字水印的竞赛指南

数据安全是信息安全领域的核心,其基础在于密码学与信息隐藏技术。密码学通过加密算法(如AES、RSA)和哈希函数,保障数据的机密性与完整性,其原理涉及对称/非对称加密、工作模式及密钥管理。数字水印技术则基于信号处理(如DCT/DWT变换),将信息嵌入多媒体载体,用于版权保护与溯源。这些技术的价值在于为数据全生命周期提供“防泄漏”与“可追溯”的解决方案,广泛应用于企业数据治理、合规审计和**数据防泄漏(DLP)** 系统。本文以一场贴近实战的**数据安全职业技能竞赛**为切入点,深入剖析如何运用密码分析、磁盘取

weixin_34314962的博客 402

ViewPager、Fragment、RadioButton实现顶部切换Tab页面

1。首先准备xml 主界面<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wid

Keep Learning , Keep Improving ! 2260

电子元器件选型与实战应用—01 电阻选型

大家好, 我是记得诚。这是《电子元器件选型与实战应用》专栏的第一篇文章,今天的主角是电阻,在每一个电子产品中,都少不了电阻的身影,其重要性不言而喻。

记得诚 3537

数学建模学习(121):Python实现模糊AHP(Fuzzy AHP)——从原理到实践

Python实现模糊AHP(Fuzzy AHP)

全栈川川 1541

android radiobutton底部导航,android中Fragment+RadioButton实现底部导航栏

在App中经常看到这样的tab底部导航栏那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现。下面我们来写一个例子首先我们先在activity_mian.xml定义布局,整个布局的外面是线性布局,上面是帧布局切换不同的Fragment,底下是RadioGroup嵌套的是RadioButton。代码如下所示:android:layout_widt...

weixin_39930276的博客 453

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Fragment点击切换界面

这是一个Fragment页面碎片public class Fra1 extends Fragment { @Nullable @Override //每个Fragment页面简单操作 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable B...

zhaochuang4858529的博客 1753

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

自定义控件RadioButton(实现5个fragmentActivity之间的切换)

1.创建一个主类WSHomeActivity继承了 FragmentActivity 2.在引入的布局文件activity_home_ws来设置控件的布局3.自定义控件RadioButton来实现(按钮+文字)的控件4.在xml文件attrs中 效果图: /**  * 完善项目APP主页面 (用FragmentManager实现tab,不可滑动)  */ public class WS

cxq_leelen的博客 1451

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Fragment + RadioGroup 实现类似于Tab的效果

*一般用于tab主页中又含有tab效果的界面中,亦可单独代替tab使用。 上效果图: 初始化的时候为界面添加一个fragment。private void init_date(){ transaction = getFragmentManager() .beginTransaction(); if (null == mSpeeddialFragme

soniky_ling的专栏 386

fragment简单的切换

布局: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match

lucky6868的博客 288

Fragment数组和RadioGroup组合实现界面的切换,没有用到fragment复用

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34275734的博客 232

Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏

  在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示:      今天我们就来实现商城底部导航栏,最终效果图如下所示:     那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现。下面我们来写一个例子   首先我们先在activity_mian.xml定义布局,整个布局的外面是线性布局,上面是帧布局...

weixin_30642305的博客 173

可能是史上最简单的Android Tab布局的实现

最近一直在纠结用何种方式来实现Tab布局呢,先前有TabActivity和ActivityGroup。前者目前已经被Google官方废弃。估计现在已经没有人用了。现在大多是通过Fragment和FragmentActivity进行实现的。有的是通过ViewPage进行实现的。总之实现方法很多。我也很纠结到底使用哪种方法去实现这个布局呢!今天就好好整理一下我认为很容易的实现这种布局的方法。这里我是使用

独孤剑客的博客 1044

Android自定义控件----RadioGroup实现APP首页底部Tab的切换

​【声明】  欢迎转载,但请保留文章原始出处→_→  生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4463931.html   【正文】 实现APP首页底部Tab的切换已经见过四五种方式了,先来看运行的效果图吧: 今天我们就用RadioGroup的方法来实现以下。   【...

weixin_33834075的博客 273
下一篇: 学习自定义view
尘彦
博客等级 码龄11年 92粉丝 22原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘彦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值