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

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/trail_good_unselect" android:state_checked="false" />
    <item android:drawable="@drawable/trail_good_select" android:state_checked="true" />
</selector>
复制代码

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/report_content_selected" android:state_checked="true" />
    //选中时图片
    <item android:drawable="@drawable/report_content_unselect" android:state_checked="false" />
    //未选中时图片
    <item android:drawable="@drawable/report_content_unselect" />//默认图片
</selector>
复制代码

然后将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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值