RadioButton排版

1.使用RadioGroup排版

RadioGroup本身只支持横向或者纵向排版,不支持多行

public class RadioGroup extends LinearLayout

可以看到RadioGroup 继承自LinearLayout,RadioGroup 的排版就是LinearLayout的排版功能;RadioGroup 只是在LinearLayout的基础上实现了,RadioButton的单选互斥功能

2.使用其他的布局控件排版RadioButton

LinearLayout
RelativeLayout
ConstraintLayout
//其他的等等

使用如ConstraintLayout等布局排版,也就意味着失去RadioGroup的单选互斥功能;需要自己来实现

单选RadioButton互斥功能实现工具类

//单选RadioButton互斥功能工具类
public class RadioButtonSelectOnlyOneHelper {private ArrayList<RadioButton> radioButtons;private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;// when true, mOnCheckedChangeListener discards eventsprivate boolean mProtectFromCheckedChange = false;private OnCheckedChangeListener mOnCheckedChangeListener;private int checkedIndex = -1;public RadioButtonSelectOnlyOneHelper() {radioButtons = new ArrayList<>();mChildOnCheckedChangeListener = new CheckedStateTracker();}public void setOnCheckedChangeListener(RadioButtonSelectOnlyOneHelper.OnCheckedChangeListener listener) {mOnCheckedChangeListener = listener;}/*** 添加单个RadioButton到分组里** @param item*/public void addRadioButton(RadioButton item) {if (item == null) return;if (item.isChecked()) {//处理有多个RadioButton的默认值为选中状态,只保留第一个mProtectFromCheckedChange = true;if (checkedIndex == -1) {checkedIndex = radioButtons.size();} else {item.setChecked(false);}mProtectFromCheckedChange = false;}item.setOnCheckedChangeListener(mChildOnCheckedChangeListener);radioButtons.add(item);}private void addView(View item) {if (item == null) return;if (item instanceof RadioButton) {addRadioButton((RadioButton) item);} else if (item instanceof ViewGroup) {addRadioButtonParent((ViewGroup) item);}}/*** 添加的ViewGroup中的RadioButton到分组里** @param radioButtonParent*/public void addRadioButtonParent(ViewGroup radioButtonParent) {if (radioButtonParent == null) return;int childCount = radioButtonParent.getChildCount();for (int i = 0; i < childCount; i++) {View view = radioButtonParent.getChildAt(i);addView(view);}}/*** 设置文本是checkedText的RadioButton为选中状态** @param checkedText*/public void setCheck(String checkedText) {Iterator<RadioButton> eIterator = radioButtons.iterator();while (eIterator.hasNext()) {RadioButton radioButton = eIterator.next();if (TextUtils.equals(radioButton.getText().toString(), checkedText)) {setCheckedRadioButton(radioButton);break;}}}/*** 获取选中的RadioButton,没有选中的就返回null** @return*/public RadioButton getCheckedRadioButton() {if (checkedIndex != -1 && checkedIndex < radioButtons.size()) {return radioButtons.get(checkedIndex);}return null;}private void setCheckedRadioButton(RadioButton checkedRadioButton) {mProtectFromCheckedChange = true;if (checkedIndex != -1) {radioButtons.get(checkedIndex).setChecked(false);}checkedIndex = radioButtons.indexOf(checkedRadioButton);checkedRadioButton.setChecked(true);mProtectFromCheckedChange = false;}/*** <p>Interface definition for a callback to be invoked when the checked* radio button changed in this group.</p>*/public interface OnCheckedChangeListener {public void onCheckedChanged(RadioButton checkedRadioButton);}private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (mProtectFromCheckedChange || !buttonView.isChecked()) {return;}setCheckedRadioButton((RadioButton) buttonView);if (mOnCheckedChangeListener != null) {mOnCheckedChangeListener.onCheckedChanged((RadioButton) buttonView);}}}
}

工具类使用

        View view = inflater.inflate(R.layout.my_layout, null);RadioButtonSelectOnlyOneHelper radioButtonSelectOnlyOneHelper = new RadioButtonSelectOnlyOneHelper();radioButtonSelectOnlyOneHelper.addRadioButtonParent(view.findViewById(R.id.radio_1));radioButtonSelectOnlyOneHelper.addRadioButtonParent(view.findViewById(R.id.radio_2));radioButtonSelectOnlyOneHelper.setOnCheckedChangeListener(new RadioButtonSelectOnlyOneHelper.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioButton checkedRadioButton) {LogUtil.e(TAG,"onCheckedChanged:"+checkedRadioButton.getText().toString());}});

radio_1和radio_2是RadioButton 的父布局控件

RadioButton样式修改

RadioButton的图标样式修改是通过配置android:button属性
RadioButton控件xml配置android:button属性

    <style name="RadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton"><item name="android:button">@drawable/checkbox_bg</item></style>

checkbox_bg.xml

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

check_checked和check_normal图片

RadioButton中引用style

                <RadioButtonandroid:id="@+id/cater"style="@style/RadioButtonStyle"android:layout_width="wrap_content"android:layout_height="@dimen/dimen_px_40"android:text="@string/cater" />

或者在对应主题中引用style

 <item name="radioButtonStyle">@style/RadioButtonStyle</item>

RadioButton文字和图标之间的间距设置

                <RadioButtonandroid:id="@+id/cater"style="@style/RadioButtonStyle"android:layout_width="wrap_content"android:layout_height="@dimen/dimen_px_40"android:text="@string/cater" />

CheckBoxStyle的style配置

    <style name="RadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton"><item name="android:button">@null</item><item name="android:drawableLeft">@drawable/checkbox_bg</item><item name="android:background">@null</item><item name="android:drawablePadding">@dimen/dimen_px_16</item></style>

原本RadioButton设置图标样式,是通过设置android:button属性;现在需要设置文字和图标之间的间距,需要android:drawableLeft和android:drawablePadding属性配合使用,使用android:drawableLeft修改图标样式,使用android:drawablePadding修改文字和图标之间的间距

最终效果:

RadioButton的排版,图标样式修改和图标文字间距修改相关推荐

  1. html桌面图标样式,Win7桌面图标样式怎么修改和还原|Win7修改和还原桌面图标样式的方法...

    电脑安装上Win7系统后,桌面图标默认图片都是一样的,可能部分人会觉得没有个性,想要修改桌面图标样式但是不知道怎么设置.其实桌面图标样式是很好修改的,如果你觉得桌面图标样式比较单一,不妨阅读下文教程进 ...

  2. (转)基于MVC4+EasyUI的Web开发框架经验总结(14)--自动生成图标样式文件和图标的选择操作...

    http://www.cnblogs.com/wuhuacong/p/4093778.html 在很多Web系统中,一般都可能提供一些图标的选择,方便配置按钮,菜单等界面元素的图标,从而是Web系统界 ...

  3. 解决论文写作排版中,两端对齐导致文字间距被word补过大的问题

    在段落中,中英文参杂,设置两端对齐,导致中英文的文字间距被word拉的过大. 解决方法:选中修改的段落, 这样设置就行....

  4. html桌面图标样式,如何更改图标样式,换桌面图标的方法

    打开桌面,桌面上摆放了放多程序的快捷方式,我们每天打开电脑最先接触的就是这些快捷方式图标.时间长了,是不是感觉乏味了.可以换一换.比如,在我的电脑桌面上有这样一个图标,那怎样更改桌面图标?下面,小编给 ...

  5. Element引入Iconfont(阿里巴巴矢量图标库)第三方图标库

    最近在使用vue-element-admin开源项目进行系统开发,但是在引入第三方图标库的时候,出现了一些问题,百度找了找,也没有找到自己想要的答案,最后经过多次尝试,将问题解决.所以特此记录一下,以 ...

  6. 使用iconfont.ttf图标文字库代替图标图片

    1 效果 2 下载字体库 1) 选中图标 -- 添加入库: 2) 添加至项目: 3) 下载项目中的图标字体库: 4)获取图标字体库iconfont.ttf 5)查看图标对应的unicode值 3 修改 ...

  7. WinForm------TreeList修改节点图标和按钮样式

    转载: https://documentation.devexpress.com/#WindowsForms/DevExpressXtraTreeListTreeList_CustomDrawNode ...

  8. Foldor for Mac(文件夹图标样式修改工具)

    如何修改文件夹图标?推荐Foldor for Mac文件夹图标样式修改工具,多达1000个图标供您选择,支持自定义调色板,需要的朋友快来试试吧! Foldor版安装教程 安装包下载完成后打开,双击.p ...

  9. Mr.Alright---安卓默认系统图标样式的修改

    安卓中默认的图标样式一共有5种,圆形,圆角,方圆形,方形,水滴形,修改的默认的方式就是overlay framework-res下的config_icon_mask字符串,值得注意的是,修改这个样式, ...

最新文章

  1. php lamp环境搭建,lamp环境搭建 --lnmp环境搭建_无需整理
  2. 只显示小方格_木托盘拼成院子围栏,镂空方格里都种上菜,一物两用,收获满满...
  3. 操作 Wave 文件(14): waveOutSetPlaybackRate、waveOutSetPitch
  4. 使用uni-app报错this.setData is not a function
  5. LAMP(7限定某个目录禁止解析php、 限制user_agent、 PHP相关配置、PHP扩展模块
  6. MySQL中引入存储引擎意义是_mysql学习九:存储引擎、存储过程和函数的引入
  7. 机器学习 来源框架_机器学习的秘密来源:策展
  8. Galaxy Nexus(i9250)卡在开机画面(‘X’画面)解决方法
  9. 【转载】三角形测试用例
  10. 局域网不能远程控制_如何通过Windows自带的局域网远程控制连接两台设备
  11. SQL获取当前时间的前10天
  12. Java dicom查看_验证DICOM文件
  13. 干货 | Python之自动化报表
  14. 蓝牙原理—Radio射频协议
  15. 传奇gee引擎,智能假人,假人脚本,geeM2假人
  16. 2021-02-22
  17. 复旦大学计算机技术非全,复旦大学软件学院非全日制研究生专业介绍
  18. MOB短信验证码开发
  19. Ubuntu-离线安装基本依赖(gcc、make、dkms等)
  20. java数独最快解_java快速解数独

热门文章

  1. 应试教育的死穴,恰在于堵死了孩子“犯错”的空间
  2. Python 列表数据类型(list)[学习 Python 必备基础知识][看此一篇就够了]
  3. 增长黑客AB-Test系统(三)——AB-Test Hash分流
  4. 【Paper Quickthrough】October Papers
  5. Y05 - 017、猜小埋年龄游戏
  6. cmd设置总是置顶_怎么将想要的窗口一直置顶 原来只要这简单的一步
  7. ppm理解与代码实现
  8. 外卖点餐系统部分模块设计
  9. Vue 下 ESLint 的 error 解决办法
  10. 爬楼梯算法 一个小孩练习爬台阶,一共10级台阶,小孩可以一次向上选择爬1-3级