Android RadioGroup动态加入RadioButton,动态设置选中和未选中的颜色及设置默认选中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Maiduoudo/article/details/84859040

此篇文章主要实现功能:RadioGroup控件根据服务端返回的数据动态添加RadioButton

关于RadioButton动态设置背景颜色和字体颜色,踩过好多坑,接下来我总结一下遇到的所有问题及解决方案,供大家参考下,希望对大家有帮助。

1.xml文件准备


首先,我们先来写radiobutton选中和未选中的背景颜色的xml文件。
在drawable文件夹下新建一个文件:button_bg.xml 
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/button_selected" ></item>
    <item android:state_checked="false" android:drawable="@drawable/button_unselected"></item>
</selector>



选中的背景颜色和未选中的背景颜色的drawable文件分别如下: 
button_selected.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white"/>
    <corners android:radius="3dp"/>
    <stroke  android:width="1dp" android:color="#FF0000"/>
</shape>


button_unselected.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/lightgray2"/>
    <corners android:radius="3dp"/>
    <stroke  android:width="1dp" android:color="@color/lightgray2"/>
</shape>


2. 代码动态设置Radiobutton选中未选中的字体颜色的xml文件


  • 注意,这里就要踩坑了,网上很多的写法跟设置背景颜色的写法一样,可以写出来发现,背景颜色对应变化了,可是字体颜色并没有对应发生变化,试了好多次,终于找到了正确的办法。 
  • 在Activity代码中设置背景颜色和字体颜色时可以发现,设置背景颜色可以直接用drawable文件夹下的资源文件:button.setBackgroundResource(R.drawable.button_bg);
  • 而设置字体颜色时,不能用R.drawable… 只能用R.color….于是,我们需要在res文件夹下新建一个color文件夹,
  • 在color文件夹下新建一个button_txt_color.xml资源文件,表示选择和未选中的字体颜色: 

如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/red" android:state_pressed="true" />
    <item android:color="@color/red" android:state_checked="true" />
    <item android:color="@color/red" android:state_focused="true" />
    <item android:color="@color/common_h1" />
</selector>

注意:这个文件中就是要注意的地方了,必须使用android:color="@color/software_textColor_selected",不能使用android:textColor属性。
这样资源文件这些就写好了,接下来就是代码应用了。

3.代码动态设置资源


在代码中设置RadioButton的背景颜色和字体颜色,并设置默认选中项。
这里就直接上代码了,都能看懂,也有注释。

 for(int i=0; i < bottomMenuList.size(); i++) {
            RadioButton radioButton = new RadioButton(getMyActivity());
            radioButton.setId(i);
            //设置RadioButton的背景
            radioButton.setBackgroundResource(R.drawable.selector_button_jingxuan);
            //设置RadioButton的文本字体颜色
            radioButton.setTextColor(getResources().getColorStateList(R.color.color_selector_textview));
            //设置按钮的样式
            radioButton.setButtonDrawable(null);
            //设置文字距离按钮四周的距离
            radioButton.setPadding(50, 0, 50, 0); 
            //设置按钮文本
            radioButton.setText(bottomMenuList.get(i).getMenu_name());
            //设置字体加粗
            TextPaint tp = radioButton.getPaint();
            tp.setFakeBoldText(true);
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(10,0,10,0);
            //容器组装
            mRgpSnackfood.addView(radioButton, params);
        }
        //设置默认值
        mRgpSnackfood.check(0);


这里就要注意了,

扫描二维码关注公众号,回复: 5442902 查看本文章
  • 一般都会以为设置默认选中项用button.setChecked(true);来实现,这样是达不到效果的,所以还是需要使用radioGroup.check(0)来设置,当前前提也必须要有button.setId(i); 这句代码。
  • radioButton.setTextColor(getResources().getColorStateList(R.color.color_selector_textview));获取颜色资源使用getResources().getColorStateList(),而不是getResources().getColor()。

好了,总结的差不多了,希望能帮助到跟我一样踩坑的朋友们。

猜你喜欢

转载自blog.csdn.net/Maiduoudo/article/details/84859040