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

在使用radiobutton  的按钮跟文字之间的间距在不同的手机上会出现间距不一致,今天学习到了如何解决这个问题:

android:button=@null;//将默认的button图片清空

android:drawableLeft=@drawable/radiobutton;//使用该属性定义button图片

android:background=@null;//将radioButton的背景设为空

android:drawablePadding=6dp;//将文字和左侧的button图片相距6dp

button/drawableLeft/background/drawablePadding结合使用方可改变文字和图片的距离 ;

一、xml文件

定义radio的样式、属性:

center_vertical

@null

0dp

@null

@drawable/package_radio_selector

10dp

package_radio_selector.xml的样式:

radioButton的使用:

style="@style/package_radio_style"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="测试"

android:textColor="@color/white"

android:textSize="15sp" />

样式的效果:

05a47d7e47059eabf5a3fa49b8335da6.png

e8b411c11db7c89961c9d7c5791b396c.png

二、java方法

使用java创建的radioButton 解决 图片与文字间距问题:

RadioButton radioButton2 = new RadioButton(this);

radioButton2.setText("测试");

radioButton2.setTextColor(getResources().getColor(R.color.white));

radioButton2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

radioButton2.setPadding(0, 0, 0, 0);

//radioButton2.setButtonDrawable(null); java设置ButtonDrawable为null,还是会显示原生的radio图标。设置为透明就好了。

radioButton2.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));

radioButton2.setBackgroundDrawable(null);

radioButton2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.package_radio_selector), null, null, null);

radioButton2.setCompoundDrawablePadding(10);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值