android scrollview 动态添加,android – 在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条评论)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值