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

ExcelVBA批量添加PDF文件

如图1所示,"BOM-01.xlsx"工作簿中的Sheet1工作表根据B列图号单元格中的内容,在图2所示的文件夹中找到对应的PDF文件,然后嵌入到相应的N列,双击N列中所示的图标,会打开PDF文件,是源文件的副本,即删除源文件,也可以打开N列的文件。 图1 根据B列的图号在N列嵌入对应的PDF文件 图2 PDF文件 图3 文件格式 Sub 导入文件() Ap...

qq389445046的博客 5577

VINS-Fusion实战:从零搭建无人机视觉SLAM系统(附ROS配置全流程)

本文详细介绍了如何从零开始为无人机搭建基于VINS-Fusion的视觉SLAM系统。内容涵盖硬件选型(重点推荐双目相机与IMU搭配)、ROS环境配置、传感器标定、核心参数调优以及实际飞行测试与问题排解全流程,旨在帮助开发者实现稳定、精确的无人机自主定位与导航。

wasm7browser的博客 214

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

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

webor2006的博客 798

C8T6和ZET6选型更换过程中出现.\Obj\Project.sct(7): error: L6235E: More than one section matches selector - cann

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、问题原因二、解决方法三、现象总结 前言 又一件离谱的事情发生了,我之前转换过程中没有发生的错误,今天拿出来程序重新编译了一下,结果他奶奶滴一个新的错误出现了,而且我还完全不知道是什么原因导致的,先上图: 一、问题原因 之前我已经尝试过更换芯片选型,但是没有出现此类问题,今天更改的时候发现了,我还以为是个偶然错误,结果换来换去,还是有这个问题,那我之前说c8t6的程序更改成zet6上跑只需改芯片选型和宏定义岂不是啪啪打.

wx18045623090的博客 1489

RadioButton不能切换问题

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

sky_castle的专栏 1175

Pytorch学习笔记(III)——提取特征

目录一、引言二、具体步骤1、参数模型2、网络结构3、参数载入4、特征提取器5、读取图片三、完整代码 一、引言   深度学习在许多任务中主要充当着特征学习的作用,而学习完的特征才是后续应用的一个关键。本文将主要介绍,如何提取任意目标层的特征图。   本文以输入数据为图片为例。 二、具体步骤 1、参数模型 博主使用了ResNet50训练了一个人脸识别的网络   训练完成的深度学习模型,我们会保存一...

lockonlxf的博客 2643

RK3588 启动卡死

kernel的设备树默认启动了hdmi0,我们的板子没有使用到hdmi0,因此在uboot阶段会卡住。uboot阶段会卡在hdptx_rpoll_cmn_config函数这里。最近再调试公司一个新板子,rk3588芯片,硬件自己设计。2、单PMIC和双PMIC配置,卡在更新处。3、emmc卡死,怀疑。1、hdmi0 卡死。

xiaoning132的专栏 5968

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

Claude Code 新手安装指南,手把手教你怎么安装使用

如果你是企业内网用户,或者需要自定义 API 端点,那可以用手动配置 API Key 的方式。

篮球界最会写bug的人;程序猿中打球最菜的人。 487

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

98DX3236内核编译过程小结

0.条件(1)Marvell技术支持提供的压缩文件:GNU EABI:arm-mv5sft-linux-gnueabi_SW3.2.tar.bz2.tar.bz2armebv7-marvell-linux-gnueabi-softfp_i686_201201.tar.bz2CPSS代码:Cpss-PP-DxCh-4.2_012.zipLinux内核文件:lsp-4.2_012.zipLSP-Uni...

snaking616 1804

把数据库算力拉满的 ABAP CDS Table Function:从语法、权限到可运行样例的全景解析

文章摘要: 本文针对ABAP开发者探讨了CDS Table Function的应用场景与实现细节。当CDS View无法处理复杂计算(如窗口函数、递归等)时,可通过Table Function将逻辑下推到HANA数据库执行。关键点包括:1)CDS实体声明与AMDP实现的协作关系;2)多租户环境下的Client处理机制(@ClientHandling.type注解);3)激活与传输的阶段性特点可能导致的"暂不可用"问题;4)基于SFLIGHT的完整示例,展示如何定义Client依赖函数、实

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 306

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

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

weixin_39930276的博客 453

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Fragment点击切换界面

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

zhaochuang4858529的博客 1753

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

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

cxq_leelen的博客 1451

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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、付费专栏及课程。

余额充值