android 动态添加组件(RadioGroup 添加RadioButton和其他组件的一些问题)
16lz
2021-01-24
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个像素,长度可控,不再自适应。
更多相关文章
- Android架构组件(Architecture Components)介绍
- android webview里设置cookie
- ImageView的属性 android:scaleType
- 一步一步学android之布局管理器——FrameLayout
- Android(安卓)TextUtil
- Android的shape的使用
- Android5.0 材质设计(纸墨设计)中的z轴理念
- Android(安卓)View - 实现流式布局
- 文本类控件(EditView 的介绍)