android relativelayout动态添加视图,在ScrollView中向RelativeLayout动态添加视图

我正在尝试将动态创建的几个RelativeLayouts添加到一个LinearLayout中,该LinearLayout位于一个RelativeLayout内,它位于ScrollView中.当所有视图的总高度超过手机屏幕的大小时,所有视图都会正确显示.但是,当动态添加的视图的总大小不足以填充屏幕时,仅显示第一个RelativeLayout元素,而其他元素不显示在屏幕中.我真的很无望,也不明白为什么.

以下是在线性布局中动态填充视图的代码:

LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);

LayoutInflater inflater = (LayoutInflater)

this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(Comment c: commentsList) {

RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(

R.layout.list_item_comment, null, false);

TextView tv = (TextView) layoutItem.findViewById(R.id.textView);

ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);

// set tv's text

// set iv's image and onclicklistener, nothing fancy here, everything goes well

commentsLayout.addView(layoutItem);

}

这是list_item_comment.xml:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/white"

>

android:id="@+id/imageView"

android:layout_width="50dip"

android:layout_height="50dip"

android:layout_alignParentLeft="true"

android:layout_marginTop="10dp"

android:layout_marginLeft="10dp"

/>

android:id="@+id/textView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_margin="10dp"

android:textSize="16sp"

android:layout_toRightOf="@id/imageView"

/>

这是此活动的xml文件:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/main_layout"

>

...

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:fillViewport="true"

android:id="@+id/scrollView"

>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/relativeContainer"

>

...

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/comments_layout"

/>

截图:

没有足够的布局:(不正确,需要显示3条评论)

0eb74da77d29190568ceacd36c71a71a.png

有足够的布局:( 正确一个,屏幕填充)

307cf19b1a9a4cb22497b7c18854298f.png

我只需要在第一种情况下显示所有三条评论:/提前谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值