Android动态加载布局(xml)AddView()时LinearLayout中View的layout_weight属性无效的解决方法

#问题描述

在如下一段代码中,布局layoutResId中有使用LinearLayout的layout_weight属性,大概布局是一个listView,然后最底部显示一个合计行,因此在ListView中使用了layout_weight="1",layout_height="0dp",如果直接使用setContentView(layoutResId)来加载这个布局,显示没有任何问题,但是通过inflate然后addView(view)来加载,发现最底部的合计行不在最底部了,而是紧跟着ListView。

View view = this.getLayoutInflater().inflate(layoutResId, null);

layout.addView(view);

#原因分析

在左查右查上查下查,终于在addView()中发现问题,在addView(view),优先使用view的LayoutParams布局参数,若没有,则使用,AddView(view, LayoutParams)方法中传入LayoutParams,若仍然没有,则会创建一个默认的LayoutParams布局参数,而创建默认的这个LayoutParams参数的宽高都是wrap_content的,所以才导致了layout_weigth不生效的原因。

#解决方案

知道问题的原因了,解决就好办了,要么给addView()的参数view手动设置LayoutParams参数,要么在使用addView(view,LayoutParams)的这个添加方法,在添加时指定LayoutParams参数,问题就解决了。

 

特此记录一下,免得忘记了。若有说的不对的地方,欢迎各位大神指正。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,你要求我模仿小红书app评论区,写一个自定义View,用xml语言实现。以下是参考代码: 在res/layout/下新建一个xml文件comment_view.xml,代码如下: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/comment_layout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 用户头像 --> <ImageView android:id="@+id/avatar" android:src="@mipmap/ic_launcher" // 头像图片资源 android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp"/> <!-- 用户名和间 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/username" android:text="用户名" android:textSize="12sp" android:textStyle="bold" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/time" android:text="2021-07-01" android:textSize="10sp" android:textColor="#999999" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp"/> </LinearLayout> <!-- 评论内容 --> <TextView android:id="@+id/comment_content" android:text="这是一条评论" android:textSize="14sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dp"/> </LinearLayout> ``` 在你的 Activity 或 Fragment 使用该自定义控件: ``` LinearLayout commentLayout = findViewById(R.id.comment_layout); LayoutInflater inflater = LayoutInflater.from(this); // 循环添加多条评论视图 for (int i = 0; i < 5; i++) { View commentView = inflater.inflate(R.layout.comment_view, null); ImageView avatar = commentView.findViewById(R.id.avatar); TextView username = commentView.findViewById(R.id.username); TextView time = commentView.findViewById(R.id.time); TextView commentContent = commentView.findViewById(R.id.comment_content); // 设置控件内容 avatar.setImageResource(R.mipmap.ic_launcher); username.setText("用户名" + i); time.setText("2021-07-0" + i); commentContent.setText("这是第" + i + "条评论"); // 添加评论视图到布局 commentLayout.addView(commentView); } ``` 以上代码,我们使用LayoutInflater动态添加5条评论View,你可以根据实际需求调整代码。希望能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值