Android(安卓)代码中动态为RadioGroup添加RadioButton
16lz
2021-01-24
在代码中new出RadioButton,并new出布局参数LayoutParams,通过layoutParams为radioButton设置margin值,再为radioButton设置其他属性即可。
RadioButton radioButton = new RadioButton(this);RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,50);layoutParams.setMargins(10, 10, 10, 10);radioButton.setLayoutParams(layoutParams);radioButton.setText("rb1");radioButton.setTextSize(12);radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮radioButton.setGravity(Gravity.CENTER);radioButton.setPadding(10, 10, 10, 10);radioButton.setTextColor(getResources().getColorStateList(R.color.selector_white_graydark_checked));//设置选中/未选中的文字颜色radioButton.setBackground(getResources().getDrawable(R.drawable.selector_radiobtn_bg));//设置按钮选中/未选中的背景group.addView(radioButton);//将单选按钮添加到RadioGroup中
注:RadioButton是RadioGroup的直接子View,使用的布局参数必须是RadioGroup.LayoutParams。
更多相关文章
- android 可自定义大小和位置的Dialog
- Java乔晓松-android使用ImageSwitcher布局的电子相册&服务器获取
- android浏览器研究-APP层UI布局
- android与C# WebService基于ksoap通信(Android篇)
- Android(安卓)学习记录-布局预览错误
- Android开发之adapter
- Android(安卓)自动化测试―robotium(五)Spinner控件
- 动态设置控件宽高
- Android(安卓)工具