我试图动态地添加创建了几个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条评论)
有了足够的布局:(正确的,画面充满)
我只需要在第一种情况下显示全部三条评论:/提前致谢。
为什么不使用一个ListView与页眉和页脚? – 2012-04-04 17:05:59
其实平时我有很多看起来像一个列表中的那些上面的其他观点:几个imageviews,按钮等。所以这不是一个常见的情况,但我需要解决这个情况也是如此,因为我不能依赖于一个事实,即每此活动中的其他视图将被初始化。有什么想法吗? – ecem 2012-04-29 15:33:55