Android RadioGroup 관련 사항

3355 단어
하나.버튼 없이 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" />//    


그리고 RadioButton에게 drawable을 드립니다.
radioButton3 = (RadioButton) findViewById(R.id.RadioButton3);
radioButton3.setButtonDrawable(ContextCompat.getDrawable(this, R.drawable.radiogroup_bg));

2.1 라디오 버튼 효과 제거
android:background="@color/transparent"

야옹 자국~
전재 대상:https://juejin.im/post/5bac9df7f265da0ac55e5898

좋은 웹페이지 즐겨찾기