java代码创建RadioButton使文字在左图标在右

首先进行创建

RadioButton radioButton = new RadioButton(mContext);

然后设置布局左右距离margin padding等

RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,RadioGroup.LayoutParams.MATCH_PARENT);
radioGroup.addView(radioButton,index,layoutParams);
layoutParams.leftMargin = 20;
layoutParams.rightMargin = 20;
radioButton.setTextSize(22);
radioButton.setPadding(10,55,10,55);
radioButton.setText(titles.get(index));

将原有的button置空

radioButton.setButtonDrawable(null);

获取并设置drawable

Drawable drawable = mContext.getResources().getDrawable(R.drawable.screen_radiobutton_drawable);
drawable.setBounds(0,0,50,50);
radioButton.setCompoundDrawables(null,null,drawable,null);

其中比较值得注意的是drawable一定要设置bounds

setCompoundDrawables可以使用这个方法是因为radioButton的最终父类是textview。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值