8

我试图动态地添加创建了几个RelativeLayouts到LinearLayout中这是一个RelativeLayout的,这是一个滚动型内内。当所有视图的总高度超过手机屏幕的大小时,所有视图都会正确显示。但是,当动态添加视图的总大小不足以填充屏幕时,只会显示第一个RelativeLayout元素,其他元素不会显示在屏幕中。我真的无望,不明白为什么。动态添加视图的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:

<RelativeLayout 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/white" 
> 
    <ImageView 
    android:id="@+id/imageView" 
    android:layout_width="50dip" 
    android:layout_height="50dip" 
    android:layout_alignParentLeft="true" 
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="10dp" 
    /> 

    <TextView 
    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" 
    /> 
</RelativeLayout> 

这里是此活动的xml文件:

<RelativeLayout 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/main_layout" 
> 
... 

    <ScrollView 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:id="@+id/scrollView" 
    > 

     <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/relativeContainer" 
     > 

     ... 

      <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/comments_layout" 
      /> 

     </RelativeLayout> 

    </ScrollView> 

</RelativeLayout> 

而且截图:

如果没有足够的布局:(不正确的,需要表现出3条评论)

Incorrect one

有了足够的布局:(正确的,画面充满)

correct one

我只需要在第一种情况下显示全部三条评论:/提前致谢。

+0

为什么不使用一个ListView与页眉和页脚? – 2012-04-04 17:05:59

+0

其实平时我有很多看起来像一个列表中的那些上面的其他观点:几个imageviews,按钮等。所以这不是一个常见的情况,但我需要解决这个情况也是如此,因为我不能依赖于一个事实,即每此活动中的其他视图将被初始化。有什么想法吗? – ecem 2012-04-29 15:33:55

回答

0

,而不是fill_parent,试着改变你的list_item_comment.xmlwrap_content<RelativeLayout>layout_height

此外,为什么您需要在您的活动的xml中的<ScrollView>以内另一个<RelativeLayout>LinearLayout足以完成你想要的活动。也许你可以删除它。

+0

我想你的建议,但它似乎并没有解决这个问题:/其实这不是我的活动的一个共同的情况下,通常必须有一个2 imageviews,一个按钮,几textviews等因此RelativeLayout的让我的工作更容易。但这也是一个例子(虽然很少见),所以我应该找到一个解决方案:/emp/ – ecem 2012-04-11 13:53:03

+0

@ecem,你能找到解决方案吗? – WindsurferOak 2013-09-29 16:33:11