Android RadioGroup相关点

一.动态添加RadioButton以及RadioButton中不带按钮

    public void addRadioButton(int size) {
        for (int i = 0; i < size; i++) {
            RadioButton radioButton = new RadioButton(this);
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, DensityUtils.dip2px(36, TryActivity.this));
            layoutParams.setMargins(0, (int) DensityUtils.dip2px(12, TryActivity.this), 0, 0);
            radioButton.setLayoutParams(layoutParams);
            radioButton.setText(trialChilds.get(i).getTitle());
            radioButton.setTextSize(17);
            radioButton.setMaxLines(1);
            radioButton.setEllipsize(TextUtils.TruncateAt.END);
            radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮
            radioButton.setBackground(ContextCompat.getDrawable(this, R.drawable.radio_trail_order_bg));
            radioButton.setGravity(Gravity.CENTER);
            radioButton.setPadding(0, 0, 0, 0);
            radioButton.setTextColor(ContextCompat.getColor(this, R.color.color7D7D7D));//设置选中/未选中的文字颜色
            mRadioGroup.addView(radioButton);//将单选按钮添加到RadioGroup中
        }
    }
复制代码

radio_trail_order_bg.xml

    "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android">
    "@drawable/trail_good_unselect" android:state_checked="false" />
    "@drawable/trail_good_select" android:state_checked="true" />

复制代码

二.改变RadioButton中的按钮样式 首先定义一个控制按钮样式的drawable

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android">
    "@drawable/report_content_selected" android:state_checked="true" />
    //选中时图片
    "@drawable/report_content_unselect" android:state_checked="false" />
    //未选中时图片
    "@drawable/report_content_unselect" />//默认图片

复制代码

然后将drawable给RadioButton

radioButton3 = (RadioButton) findViewById(R.id.RadioButton3);
radioButton3.setButtonDrawable(ContextCompat.getDrawable(this, R.drawable.radiogroup_bg));
复制代码

2.1去掉RadioButton的点击效果

android:background="@color/transparent"
复制代码

喵印~~

转载于:https://juejin.im/post/5bac9df7f265da0ac55e5898

你可能感兴趣的:(Android RadioGroup相关点)