有时需要在RadioGroup中动态添加RadioButton

1.如果不做设置,那么RadioButton之间就会贴在一起,动态设置margin

2.必须给RadioButton添加ID,否则默认选中的选项会一直是选中状态

        RadioGroup rg= (RadioGroup) view.findViewById(R.id.rg_layout_radioButton);//横向        for (int i = 0; i < mTextArray.length; i++) {            //radioButton            RadioButton radioButton = new RadioButton(context);            int dimension = (int) (getResources().getDimension(R.dimen.qb_px_10)+0.5f);//会自动转化为像素值            radioButton.setPadding(dimension,0,0,0);            radioButton.setButtonDrawable(R.drawable.selector_icon_31_32);            radioButton.setText(mTextArray[i]);            radioButton.setTextColor(getResources().getColor(R.color.white));            //必须有ID,否则默认选中的选项会一直是选中状态            radioButton.setId(i);            if (i==mTextArray.length-1){                //默认选中                radioButton.setChecked(true);            }            //layoutParams 设置margin值            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,                    ViewGroup.LayoutParams.WRAP_CONTENT);            if (i!=0){                int i1 = (int) (getResources().getDimension(R.dimen.qb_px_40) + 0.5f);                layoutParams.setMargins(i1,0,0,0);            }else {                layoutParams.setMargins(0,0,0,0);            }            //注意这里addView()里传入layoutParams            rg.addView(radioButton,layoutParams);        }

 

更多相关文章

  1. Android(安卓)文本输入框 获取焦点和设置光标到末尾
  2. Android的图形与图像处理之一 使用简单图片&绘图
  3. Android(安卓)ListView+CheckBox实现全选、单选
  4. Ubuntu下Android(安卓)Studio Gradle sync failed peer not auth
  5. android 指定打包资源文件的方法
  6. Android(java)学习笔记124:Android权限大全
  7. Android(安卓)Gradle 使用技巧
  8. Android(安卓)启动模式
  9. Android(安卓)RadioGroup设置单选效果

随机推荐

  1. Android平台架构及特性
  2. android实现分享给好友功能
  3. ImageView属性相关
  4. Android(安卓)Studio 100 tips and trick
  5. AndroidManifest.xml 详解 (五) 之uses-f
  6. Android的消息机制(一)
  7. 让Camera在portrait模式下不旋转90度
  8. 转:Android更换皮肤-Theme方式
  9. 开发者不可错过的开源工具 —— Android(
  10. Unity中关于保存图片到Android/IOS相册中