代码设置LinearLayout的高度

敌花师 2014-09-17 03:56:44
我想把这个LinearLayout宽度设置成为FILL_PARENT,源码如下
LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.checkboxdoitem, null);
LayoutParams params= (LayoutParams) checkboxLinearLayout.getLayoutParams();
params.width=LayoutParams.FILL_PARENT;
checkboxLinearLayout.setLayoutParams(params);
红色处报错,log是下图
...全文
589 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
danielinbiti 2014-09-17
  • 打赏
  • 举报
回复
LayoutParams params= (LayoutParams) checkboxLinearLayout.getLayoutParams();//没有set先get所以获取的是null 应该 checkboxLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
敌花师 2014-09-17
  • 打赏
  • 举报
回复
引用 6 楼 hjywyj 的回复:
checkboxLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 还不如写在xml里呢
我在LinearLayout动态添加这个checkboxdoitem,xml内容我上面发了,我想自动换行,结果都在一行,我就想4个一行,强行的设置第四个的宽度为fill_parent,可是效果还是都在一行,不会设计这个android视图,怎么破
  • 打赏
  • 举报
回复
checkboxLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 还不如写在xml里呢
gqjjqg 2014-09-17
  • 打赏
  • 举报
回复
引用 4 楼 oBehavior 的回复:
[quote=引用 3 楼 heaimnmn 的回复:] params为空值,没获取到
这个是checkboxdoitem的xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <CheckBox android:id="@+id/checkBoxItem1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> [/quote] inflate 出来的View 没add到显示的布局中之前,布局参数是获取不到的。
敌花师 2014-09-17
  • 打赏
  • 举报
回复
引用 3 楼 heaimnmn 的回复:
params为空值,没获取到
这个是checkboxdoitem的xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <CheckBox android:id="@+id/checkBoxItem1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
哎,真难 2014-09-17
  • 打赏
  • 举报
回复
params为空值,没获取到
gqjjqg 2014-09-17
  • 打赏
  • 举报
回复
params 为空?
敌花师 2014-09-17
  • 打赏
  • 举报
回复
大神在哪里啊params.width直接设置为50也报错啊

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧