【学习笔记】Android 动态添加控件 getlayoutparams 为空的问题
来源:互联网 发布:seo怎么做优化 编辑:程序博客网 时间:2024/05/16 05:29
问题描述:
在xml里面添加一个RadioGroup控件,在代码里面动态的给RadioGroup添加子控件RadioButton同时,给RadioButton之间设置间隔
for (int i = 0; i < length; i++) {RadioButton radioButton = new RadioButton(getApplicationContext());radioButton.setText(Constants.feedbacks[i]);radioButton.setTextColor(Color.BLACK);radioButton.setButtonDrawable(R.drawable.xianiu_common_radio_btn_selector);LinearLayout.LayoutParams mLayoutParams = (LayoutParams) radioButton.getLayoutParams();mLayoutParams.setMargins( 0, 10, 0, 10);radioButton.setLayoutParams(mLayoutParams);radiogroup.addView(radioButton);}
这段代码在运行时报错,因为 这句 mLayoutParams = (LayoutParams) radioButton.getLayoutParams();
mLayoutParams为null
经过一番查找才发现原来RadioButton只是被创建出来,还没有被添加到view中所以暂时获取不到LayoutParams
要先
edittext_radiogroup.addView(radioButton);
然后才可以
LinearLayout.LayoutParams mLayoutParams = (LayoutParams) radioButton.getLayoutParams();
for (int i = 0; i < length; i++) {RadioButton radioButton = new RadioButton(getApplicationContext());radioButton.setText(Constants.feedbacks[i]);radioButton.setTextColor(Color.BLACK);radioButton.setButtonDrawable(R.drawable.xianiu_common_radio_btn_selector);edittext_radiogroup.addView(radioButton);LinearLayout.LayoutParams mLayoutParams = (LayoutParams) radioButton.getLayoutParams();mLayoutParams.setMargins( 0, 10, 0, 10);radioButton.setLayoutParams(mLayoutParams);}
运行正常,问题解决了。
-------------------------------------------------可爱的分割线---------------------------------------------------------------------
前面写的只是添加几个RadioButton, 那么怎么添加点击事件呢?
RadioGroup的选中事件怎么写呢?
我们可以从
mGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
}
获取checkedId 。但是我们获取的checkedId有时候是很大的数字怎么办呢?
解决办吧:
RadioButton radioButton = new RadioButton(getApplicationContext());
radioButton.setTag(i);给radioButton设置tag
然后在选中事件里面获取tag 。
-----------------------------15.11.13编辑-----------------------------------------------
RadioButton 添加自定义图片之后,图片和文字之间的距离控制起来比较麻烦,因为API17之前的控制方法发生了变化。
直接上代码:
if(version >= 17){button.setPadding(20, 0, 10, 0);}else{button.setCompoundDrawablePadding(10);}
0 0
- 【学习笔记】Android 动态添加控件 getlayoutparams 为空的问题
- Android开发学习笔记:为动态添加的EditText控件绑定值改变监听事件
- Android开发学习笔记:为动态添加的Spinner控件绑定数据源以及监听事件
- 关于android api的 getLayoutParams()方法报空指针异常!
- Android学习笔记---关于获取极光IDgetRegistrationID为空的问题
- Javascript动态为Select控件添加项并设置控件值无效的问题
- android 动态添加控件控制属性问题
- Android动态设置布局,为LinearLayout动态添加子控件
- 动态控件添加问题
- 关于Android Studio使用butterknife:8.5.1注解是,控件总是为空的问题
- Android学习笔记之为Dialog自定义布局,并说明空指针问题
- VB动态添加控件的问题
- Android 动态添加控件
- Android--动态添加控件
- Android 动态添加控件
- android --动态添加控件
- android 动态添加控件
- android 动态设置控件的大小 和 动态添加控件
- hibernate
- Quartz
- 根据坐标采色修改背景
- unix编程学习笔记-------对于消息队列的本质的疑问????
- Ant
- 【学习笔记】Android 动态添加控件 getlayoutparams 为空的问题
- Maven
- nexus私服
- Tomcat
- Cannot create JDBC driver of class '' for connect URL 'null'
- 推荐 7 款开源密码管理工具
- 二维卷积面试题
- 单行文字cell的创建,可重复利用cell
- Android