LinearLayout addView添加视图无法正常工作

6

我在使用LinearLayout的addView方法时遇到了问题。我不知道为什么,但是如果我添加三个视图,只有第一个视图会显示出来。以下是代码:

Comment[] comments = posts[position].getComments();
LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.post_list_item_comments);
layout.removeAllViews();
for(int i=0; i<comments.length; i++) {
    View comment = inflater.inflate(R.layout.post_list_item_comment,null);
    ((TextView)comment.findViewById(R.id.post_list_item_comment_name)).setText(comments[i].getFrom().getName());
    ((TextView)comment.findViewById(R.id.post_list_item_comment_message)).setText(comments[i].getText());
    layout.addView(comment,i);
}

我也尝试使用addView(comment),但结果相同。

这是我使用findViewById方法检索到的视图代码。

<LinearLayout
    android:id="@+id/post_list_item_comments"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="40dip"
    android:layout_below="@id/post_list_item_footer_text"
    android:visibility="gone"/>

这是我需要解析的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <View
        android:id="@+id/post_list_item_comment_divider"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/divider"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="2dip"/>
    <ImageView
        android:id="@+id/post_list_item_comment_photo"
        android:layout_width="40dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/post_list_item_comment_divider"
        android:adjustViewBounds="true"/>
    <TextView
        android:id="@+id/post_list_item_comment_name"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@id/post_list_item_comment_photo"
        android:layout_below="@id/post_list_item_comment_divider"
        android:maxLines="2"
        android:ellipsize="end"/>
    <TextView
        android:id="@+id/post_list_item_comment_message"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@id/post_list_item_comment_photo"
        android:layout_below="@id/post_list_item_comment_name"
        android:textSize="13sp"
        android:maxLines="2"
        android:ellipsize="end"/>
</RelativeLayout>
2个回答

20

你没有声明LinearLayout的方向...默认情况下是水平的,请尝试设置为垂直方向

<LinearLayout
    android:id="@+id/post_list_item_comments"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="40dip"
    android:layout_below="@id/post_list_item_footer_text"
    android:visibility="gone"/>

谢谢!你是对的。我不知道为什么我认为默认是垂直的。 - Adrian

2
使用层次结构查看器检查是否确实只添加了一个视图,或者您只能看到一个视图。例如,如果您重复使用此行android:layout_below="@id/post_list_item_footer_text",可能会出现问题?我不知道预期的行为...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接