关于RadioButton动态设置背景颜色和字体颜色,踩过好多坑,接下来我总结一下遇到的所有问题及解决方案,供大家参考下,希望对大家有帮助。
1.首先,我们先来写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">
<!-- 填充 -->
<solid android:color="@color/red" />
<!-- 圆角 -->
<corners android:radius="5dp" />
</shape>
button_unselected.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 填充 -->
<solid android:color="@color/white" />
<stroke android:color="@color/red" android:width="1px"/>
<!-- 圆角 -->
<corners android:radius="5dp" />
</shape>
2.接下来我们就写radiobutton选中和未选中的字体颜色的xml文件。
注意,这里就要踩坑了,网上很多的写法跟设置背景颜色的写法一样,可以写出来发现,背景颜色对应变化了,可是字体颜色并没有对应发生变化,试了好多次,终于找到了正确的办法。
我们在Activity代码中设置背景颜色和字体颜色时可以发现,设置背景颜色可以直接用drawable文件夹下的资源文件:button.setBackgroundResource(R.drawable.button_bg);而设置字体颜色时,不能用R.drawable… 只能用R.color….于是,我们需要在value文件夹下新建一个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="#ffffff" android:state_checked="true"></item>
<item android:color="#f00" android:state_checked="false"></item>
</selector>
这样资源文件这些就写好了,接下来就是Activity中的代码了:
3.在代码中设置RadioButton的背景颜色和字体颜色,并设置默认选中项。
这里就直接上代码了,都能看懂,也有注释。
public class MainActivity extends AppCompatActivity {
private RadioGroup mAllLayout;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<>();
data.add("近视眼镜");
data.add("太阳眼镜");
data.add("功能眼镜");
data.add("隐形眼镜");
data.add("男款眼镜");
data.add("女款眼镜");
data.add("品牌眼镜");
initView();
}
private void initView() {
mAllLayout = (RadioGroup) findViewById(R.id.all_layout);
for (int i = 0; i < data.size(); i++) {
RadioButton button = new RadioButton(this);
button.setBackgroundResource(R.drawable.button_bg);
button.setTextColor(getResources().getColorStateList(R.color.button_txt_color));
//这里给每个button设置Id,后面才能用mAllLayout.check(0);来设置默认选中项
button.setId(i);
//设置不要单选按钮前面的小圆圈
Bitmap a=null;
button.setButtonDrawable(new BitmapDrawable(a));
//设置字体加粗
TextPaint tp = button.getPaint();
tp.setFakeBoldText(true);
button.setPadding(20,10,20,10);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
mAllLayout.addView(button, params);
}
mAllLayout.check(0);
}
}
这里就要注意了,一般都会以为设置默认选中项用button.setChecked(true);来实现,这样是达不到效果的,所以还是需要使用radioGroup.check(0)来设置,当前前提也必须要有button.setId(i); 这句代码。
好了,总结的差不多了,希望能帮助到跟我一样踩坑的朋友们,如果帮助到你,记得顶一个哦,如果有更好方法的,也希望朋友们在下面回复一下,毕竟我的技术还有限,也希望跟大家多学习学习。。