RadioButton 左侧显示文字,右侧显示按钮时文字不靠边的问题解决

Android RadioButton字体在左边的两种方法 有时候需要把字体放在radio的左边该怎么做呢,其实有两种方法可供选择: 第一种: <RadioGroup android:id="@+id/desktheme_type" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/desktheme_title" android:or 阅读详情

项目中有一个这样的需求:


下面三行点击某行即选中,颜色变深。自然的想到使用RadioButton因此决定使用RadioButton和RadioButton实现。


1、RadioButton实现上述效果

        <RadioButton
            android:id="@+id/rbAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目一"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />
这个是RadioButton的实现:首先android:button="@null",这个用于隐藏RadioButton默认的按钮;android:drawableRight="@drawable/selector_tb"这个用于显示自己定义的按钮,也就是上图右侧的按钮,当然,如果在左侧,你可以设置android:drawableLeft="@drawable/selector_tb"等;然后就是文字的属性。这样就设置完毕了。android:drawableRight="@drawable/selector_tb"中的selector_tb是一个选择器,代码如下:

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


完整的代码如下:

布局文件activity_main.xml:

<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"
    android:background="@android:color/white"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RadioGroup
        android:id="@+id/rgRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/rbAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目一"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbLimit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目二"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbNone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目三"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />
    </RadioGroup>

</LinearLayout>

MainActivity.java:

public class MainActivity extends Activity implements OnCheckedChangeListener {
	private RadioGroup rgRight;
	private RadioButton rbAll, rbLimit, rbNone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		rgRight = (RadioGroup) findViewById(R.id.rgRight);
		rgRight.setOnCheckedChangeListener(this);
		rbAll = (RadioButton) findViewById(R.id.rbAll);
		rbLimit = (RadioButton) findViewById(R.id.rbLimit);
		rbNone = (RadioButton) findViewById(R.id.rbNone);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rbAll:
			radioButtonInit();
			rbAll.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
			break;
		case R.id.rbLimit:
			radioButtonInit();
			rbLimit.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
			break;
		case R.id.rbNone:
			radioButtonInit();
			rbNone.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
			break;
		default:
			break;
		}
	}

	private void radioButtonInit() {
		rbAll.setTextColor(getResources().getColor(android.R.color.primary_text_light));
		rbLimit.setTextColor(getResources().getColor(android.R.color.primary_text_light));
		rbNone.setTextColor(getResources().getColor(android.R.color.primary_text_light));
	}

}

2、问题

上面的代码运行后效果如下:


上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边。试了各种方法都不行。

最后,无意中给RadioButton添加一个backgroud属性即可:

<RadioButton
            android:id="@+id/rbAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目一"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

最后实现了所需效果。


3、总结

虽然效果实现了,但是这个问题一直不明白为什么,怀疑可能是RadioButton没有真正的match_parent。因此写了下面的测试代码:

<RadioGroup
        android:id="@+id/rgRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/rbAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目一"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:paddingLeft="0dp"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目一"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbLimit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:paddingLeft="4dp"
            android:text="测试条目二"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbNone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:padding="0dp"
            android:text="测试条目三"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />

        <RadioButton
            android:id="@+id/rbNone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:button="@null"
            android:drawableRight="@drawable/selector_tb"
            android:text="测试条目三"
            android:textColor="@android:color/primary_text_light"
            android:textSize="14sp" />
    </RadioGroup>
运行后效果如下:



通过对比,感觉应该是左面有padding。尝试去打印下它的paddingleft:getCompoundPaddingLeft(),得到的结果是76,说明左侧是有边距的。但是,为什么设置background也行呢?高手请指点,谢谢~~





陈小胖:RadioButton底部按钮文字图片背景的颜色改变 文章目录1、效果图2、xml3、改变背景图片4、改变文字颜色5、改变背景颜色寄语 1、效果图 只是简单写了一个点击文字变色 图片变色 背景变色 2、xml 四个重复太多 所以只写了一个RadioButton 注意:RadioButton写在RadioGroup里 <RadioButton android:id="@+id/btn_one" ... 阅读详情

相关推荐

Vue3 - Element Plus 组件库让 [el-radio 单选框] 与 [el-checkbox 复选框] 由 “横向排列“ 改为 “纵向排列“ 显示(将单选框/多选框设置为纵向排列效果)

vue3elementplus组件库,vue3el-radio单选框el-checkbox复选框改成纵向排序排列,vue3elementplus单选框el-radio如何改为纵向排列,vue3el-radio单选框设置成纵向横排,vue3如何把elementplusel-checkbox多选框复选框改成并列纵向排放,elementplus单选框复选框如何竖着排列,vue3elementplus单选框el-radio/复选框el-checkbox如何把每个选项另起一行显示,e

🏆 前端领域优质创作者 4409

Android RadioButton 文字在左边,圆圈在右边

RadioButton 文字在左边,圆圈在右边 转载出处:https://www.jianshu.com/p/2ed496c97c53 在xml文件的RadioButton控件上加上属性: android:layoutDirection="rtl" android:textDirection="ltr" ...

风火神 1370

KiCad插件安装太慢?试试华秋版,再手把手教你装这5个实用插件

本文介绍了KiCad华秋发行版如何解决插件安装慢的问题,并详细推荐了5个实用插件及其安装方法。华秋版优化了国内用户的下载速度,预装了常用扩展包,包括BOM生成器、3D模型增强工具等,显著提升PCB设计效率。

weixin_29184075的博客 549

Android中让RadioButton 文字在左边,选择按钮在右边

RadioButton默认选择框是在左边显示的,文字显示在右边,可是在某些场景下,我们想在右边显示选择框,左边显示文字,这时候就需要我们去设置一些属性了,给RadioButton 添加一下两个属性就可实现这种效果 android:layoutDirection="rtl" android:textDirection="ltr" <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http:/..

yidabashou的博客 5422

别再折腾老教程了!树莓派4B/5最新系统(Raspberry Pi OS Bookworm)红外遥控配置,用gpio-ir驱动一次成功

本文详细介绍了树莓派4B/5在最新Raspberry Pi OS Bookworm系统下配置红外遥控的完整指南,摒弃过时的lirc-rpi驱动,采用现代gpio-ir驱动方案。从硬件连接到软件配置,提供10分钟快速上手指南,涵盖驱动加载、信号测试、按键映射及高级应用场景,帮助用户高效完成红外遥控配置。

weixin_42520909的博客 442

gesp c++ 八级知识点

gesp c++ 八级知识点

MMD.然大大 2250

Android RadioButton左侧显示文字,右侧显示Button

RadioButton左侧显示文字及右侧显示Button 现在新翻新的项目遇到了一个RadioButton左侧显示文字及右侧显示button的设计,遗憾的,度娘没能帮到我 stackoverflow中记录了一个方法 <RadioButton android:id="@+id/item_list_radiobtn" android:layout_wi...

现状很好 1189

我想自己组装一台服务器,微调大模型通义千问2.5 Omni 72B,但是我是个人购买,资金非常有限,最省的方案

阶段动作✅ 推理用 GGUF + llama.cpp + 4090 加载 14B INT4✅ 微调训练 Qwen2.5-7B-Chat,使用 LoRA / QLoRA❌ 放弃Qwen2.5-Omni-72B 全量微调(显存需求 >160G,不现实)

AngelCryToo的专栏 2830

(转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决

作者:发布日期:2014-02-13 21:00:45 我来说两句(0) 0 Tag标签:RadioButton左侧显示 项目中有一个这样的需求: 下面三行点击某行即选中,颜色变深。自然的想到使用RadioButton因此决定使用RadioButton和RadioButton实现。 1、RadioButton实现上述效果 ...

weixin_30632883的博客 377

华为OD技术面真题 - JAVA开发 - 1

汇总华为OD技术面八股JAVA八股,高频技术真题并提供详细解答。

qq_45776114的博客 1206

UG NX二次开发(C#)--详细讲述编程模板NXOpen_CS_Wizard的代码意义,帮你轻松进入NXOpen(C#)二次开发

有几个读者私信问我,在看了“NX1957在VS2019上二次开发的环境配置”后,能完成了UG NX二次开发(C#)模板的配置,也能根据模板创建工程,但是对工程文件.cs不甚理解;并且,在UG Styler新建完成后,其也有一个dlx文件和.cs文件,也不清楚怎么使用。本文先对UG NX二次开发(C#)模板文件进行讲解,在下一个博客中,讲解Styler新建后的模板文件。以上就是对编程模板NXOpen_CS_Wizard(c#)的讲解,其实只要明白了逻辑关系,编程实现只是时间问题,祝你好运。

GimiGimmy 1788

RadioButton左侧显示文字及右侧button对齐

想要实现的效果: 一、将RadioButton显示在文字的右侧,只需在布局文件中设置两个属性: android:button="@null" android:drawableRight="@android:drawable/btn_radio" 二、想让位于文字右侧的RadioButton对齐,只需要给所有RadioButton设置相同的固定宽度,

╃緣分天空╃ 8590

异常检测在大数据中的10个典型应用场景解析

异常检测是大数据时代的“业务听诊器”,能帮你从海量数据中定位问题、规避风险、发现机会;10个典型场景覆盖了运维、风控、电商、工业、医疗、能源等行业,几乎所有行业都需要异常检测;理解业务中的“正常模式”选择复杂的算法;通用问题的解决思路:根据数据类型选算法、处理不平衡数据、用业务指标评估效果、实现实时检测。通过本文的10个场景,你应该已经明白:异常检测不是玄学,而是能直接解决真实问题的工具。运维人员用它提前预警宕机;金融机构用它拦截欺诈;工厂用它预测设备故障;医院用它预警危重病人。

2501_92132293的博客 529

Android RadioButton左侧显示文字及右侧显示Button

Android RadioButton左侧显示文字及右侧显示Button Android RadioButton左侧显示文字及右侧显示Button.

qq_33521481的博客 1016

情感分析在金融领域的应用:舆情影响股价预测

我们都知道,股价的波动受到多种因素的影响,而大众的情绪和看法就是其中很重要的一部分。我们在日常生活中都有根据别人的评价来判断事物好坏的经验,在金融市场里,舆情就像是大众对某家公司的集体评价,它能反映出市场参与者的情绪和预期。学习这方面的知识,能帮助投资者更好地把握市场动态,做出更明智的投资决策,无论是个人投资者还是金融机构的分析师,都能从中受益。有一天,你看到某家知名企业被曝光了严重的产品质量问题,新闻铺天盖地,股民们也都在热议纷纷,负面情绪弥漫。这时,你会不会觉得这家公司的股价可能要下跌了呢?

AI 原生应用开发的博客 1148

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

当radiobutton字数不一样时radiobutton显示不全

出问题代码: radiogroup = (RadioGroup) findViewById(R.id.radioGroup1); radio_twoHour = (RadioButton) findViewById(R.id.radio_twohour); radio_nextDay = (RadioButton) findViewById(R.id.radio_nextday); rad

u010015933的博客 2253

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

android radiobutton 文字按钮的距离,android RadioButton 图片与文字间距问题

在使用radiobutton的按钮跟文字之间的间距在不同的手机上会出现间距不一致,今天学习到了如何解决这个问题:android:button=@null;//将默认的button图片清空android:drawableLeft=@drawable/radiobutton;//使用该属性定义button图片android:background=@null;//将radioButton的背景设为空...

weixin_35987118的博客 2267

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Android中如何设置RadioButton在文字的右边,图标在左边

Android中如何设置RadioButton在文字的右边,图标在左边? 现状:Android 默认的 RadioGroup 中 RadioButton和 文字 Text 的位置关系是 Radiobutton在文字左边。且只有点击Radiobutton才能触发点击事件,文字不能触发监听事件。 目标:外观UI上要和 RadioGroup 的保持一致,但是逻辑上除了要实现单选功能外,还要能根据逻辑

abcdeudi的专栏 3816

android RadioButton 图片与文字间距问题

在使用radiobutton  的按钮跟文字之间的间距在不同的手机上会出现间距不一致,今天学习到了如何解决这个问题: android:button=@null;//将默认的button图片清空 android:drawableLeft=@drawable/radiobutton;//使用该属性定义button图片 android:background=@null;//

wuyou1336的博客 2万+

Android RadioButton设置选中时文字和背景颜色同时改变

在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。 首先,在布局文件中新建一组 RadioButton : <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_

菜鸟的自我修养 7万+

android设置按钮在最右,Android设置RadioButton在文字的右边

encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" >android:id="@+id/...

weixin_29081703的博客 775

单选按钮RadioButton使用

1、单选按钮使用 在默认情况先,单选按钮显示为一个圆形图标,并且在该图标旁边放置一些说明性文字。在程序中,一般将多个单选按钮放置在按钮组里面,使用这些按钮表现出来某些功能,当用户选中某个单选按钮后,按钮组中的其他按钮将被自动取消选中状态。在Android中,单选按钮使用RadioButton表示,而RadioButton有是Button的子类,所有单选按钮可以直接使用Button支持的各种属性。 在Android中,可以使用两种方法向屏幕中添加单选按钮:一种是通过XML布局文件中使用标记添加;另一种是在Ja

chinaherolts2008的博客 3125

RadioButton里面的文字对齐问题解决

今天遇到了一个问题,已经设置了RadioButton里面的元素居中对齐的属性,但显示出来却不是对齐的, <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="

luoyang2的博客 4237

Android Studio 单选按钮RadioButton

功能 从多种选择中选择一个,需要单选按钮,经典场景是选择性别男、女。 显示 单选按钮需要放到单选按钮组RadioGroup中,每组中只有一个元素可以被选中。RadioGroup常用属性有: check,设置选中按钮的资源编号 getCheckedRadioButtonId,获取选中按钮的资源编号 实例代码如下: <?xml version="1.0" encoding="utf-8"?&...

编程副队长 6212

在Android中搭建图标+文字的按钮显示

在Android中搭建图标+文字的按钮显示效果如下图: 添加图标: 使用RadoiGroup来包含三个RadioButton: <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:backgr...

龙文浩的博客 1779
上一篇: android网络开源框架volley(三)——请求队列是主线
下一篇: android网络开源框架volley(四)——谈谈图片加载
ttdevs
博客等级 码龄15年 388粉丝 91原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值