您的位置:首页 > 移动开发 > Android开发

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

2017-01-03 14:46 609 查看
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个像素,长度可控,不再自适应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息