在代码中,子view设置的属性在viewGroup中无效的问题解决
2016-01-11 15:08
369 查看
<span style="font-family:SimHei;font-size:14px;"> RadioGroup myRadioGroup = new RadioGroup(this); myRadioGroup.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); myRadioGroup.setOrientation(LinearLayout.HORIZONTAL); for (int i = 0; i < 3; i++) { RadioButton mRadioButton = new RadioButton(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 0, 100); params.weight = 1.0f; params.gravity = Gravity.CENTER; mRadioButton.setLayoutParams(params); mRadioButton.setText(""+i); myRadioGroup.addView(mRadioButton); }</span>
需求是创建3个横向显示的单选按钮,并且这三个按钮的宽度平分屏幕宽度,上面的写法是错误的,设置的params属性并不会有效果,因为params中的一些属性是相对于父布局设置的,例如这个params.weight = 1.0f; 而此时这个单选按钮并没有一个父布局,所以应该改成:
<span style="font-family:SimHei;font-size:14px;">for (int i = 0; i < 3; i++) { RadioButton mRadioButton = new RadioButton(this); myRadioGroup.addView(mRadioButton); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 0, 100); params.weight = 1.0f; params.gravity = Gravity.CENTER; mRadioButton.setLayoutParams(params); mRadioButton.setText(""+i); }</span>
相关文章推荐
- Spring粘合iBatis时多种配置SqlMapConfig.xml路径的方法
- Python 装饰器(decorator )的深度解读
- C++之文件IO
- Django开发博客(六)——添加markdown支持
- Python之paramiko模块和SQL连接API
- 基于注解的Spring AOP的配置和使用
- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined ;At least one of these environment variable is needed to run this program
- Lua中的模块与module函数详解
- 中介者模式 C#
- Elasticsearch基础教程
- Lua模块和模块载入浅析
- 【第七章】 对JDBC的支持 之 7.3 关系数据库操作对象化 ——跟我学spring3
- Python:使用threading模块实现多线程(转)
- Javaweb学习总结(三):Myeclipse常用的快捷键
- Python学习手册 - 08
- windows64位下使用python2遇到找不到usage init_tcl的问题
- java过滤器和监听器详解
- Eclipse 中关闭 js 错误提示
- java实现DES数据加密与解密
- C++学习笔记