android – 在ScrollView中动态地向RelativeLayout添加视图

前端之家收集整理的这篇文章主要介绍了android – 在ScrollView中动态地向RelativeLayout添加视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将动态创建的几个RelativeLayouts添加到LinearLayout中,该LinearLayout位于RelativeLayout内部,位于ScrollView内部.当所有视图的总高度超过手机屏幕的大小时,所有视图都会正确显示.但是,当动态添加的视图的总大小不足以填充屏幕时,仅显示第一个RelativeLayout元素,而其他元素不显示在屏幕中.我真的很无望,也不明白为什么.

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

  1. LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);
  2.  
  3. LayoutInflater inflater = (LayoutInflater)
  4. this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  5.  
  6. for(Comment c: commentsList) {
  7.  
  8. RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(
  9. R.layout.list_item_comment,null,false);
  10.  
  11. TextView tv = (TextView) layoutItem.findViewById(R.id.textView);
  12. ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);
  13.  
  14. // set tv's text
  15. // set iv's image and onclicklistener,nothing fancy here,everything goes well
  16.  
  17. commentsLayout.addView(layoutItem);
  18. }

这是list_item_comment.xml:

  1. <RelativeLayout
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@color/white"
  6. >
  7. <ImageView
  8. android:id="@+id/imageView"
  9. android:layout_width="50dip"
  10. android:layout_height="50dip"
  11. android:layout_alignParentLeft="true"
  12. android:layout_marginTop="10dp"
  13. android:layout_marginLeft="10dp"
  14. />
  15.  
  16. <TextView
  17. android:id="@+id/textView"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:layout_margin="10dp"
  21. android:textSize="16sp"
  22. android:layout_toRightOf="@id/imageView"
  23. />
  24. </RelativeLayout>

这是此活动的xml文件

  1. <RelativeLayout
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:id="@+id/main_layout"
  6. >
  7. ...
  8.  
  9. <ScrollView
  10. android:orientation="vertical"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:fillViewport="true"
  14. android:id="@+id/scrollView"
  15. >
  16.  
  17. <RelativeLayout
  18. android:orientation="vertical"
  19. android:layout_width="fill_parent"
  20. android:layout_height="fill_parent"
  21. android:id="@+id/relativeContainer"
  22. >
  23.  
  24. ...
  25.  
  26. <LinearLayout
  27. android:orientation="vertical"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:id="@+id/comments_layout"
  31. />
  32.  
  33. </RelativeLayout>
  34.  
  35. </ScrollView>
  36.  
  37. </RelativeLayout>

截图:

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

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

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

解决方法

而不是fill_parent,尝试更改< RelativeLayout>的layout_height.你的list_item_comment.xml到wrap_content.

另外,为什么还需要另一个< RelativeLayout>在你的< ScrollView>里面您的活动的xml. LinearLayout足以完成您希望活动的样子.也许你可以删除它.

猜你在找的Android相关文章