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。

猜你喜欢

转载自blog.csdn.net/weixin_39339407/article/details/84070280
今日推荐