android 动态添加组件(RadioGroup 添加RadioButton和其他组件的一些问题)

来源:互联网 发布:帮助记忆的软件 编辑:程序博客网 时间:2024/05/16 07:13

android动态添加组件,在项目中会经常使用到,首先罗列一下是我自己遇到的一些问题及解决办法
一、 曾经遇到一个问题解决了好久,(RadioGroup 添加RadioButton和其他组件),当RadioGroup动态添加非RadioButton时有时该组件的宽度会默认为wrapcontent,即使你使用了matchparent。
此时我们可以把宽度设为定值,避免其自动wrap

RadioGroup rg = new RadioGroup(getContext());rg.setOrientation(LinearLayout.VERTICAL);//垂直方向ViewGroup.LayoutParams rg_lp = new ViewGroup.LayoutParams(600, RadioGroup.LayoutParams.WRAP_CONTENT);//rg的宽度设值为600,确定的值rg.setLayoutParams(rg_lp);LinearLayout.LayoutParams lp_et = new LinearLayout.LayoutParams(600, 100);//et的长宽设置final EditText et = new EditText(getContext());et2.setLayoutParams(lp_et2);rg.addView(rb);rg.addView(et);

此时,edittext的长度将为600个像素,长度可控,不再自适应。

1 0
原创粉丝点击