2011-02-24 116 views
6

我遇到了LinearLayout的addView方法問題。我不知道爲什麼,但如果我添加三個視圖,只顯示第一個視圖。這裏是代碼: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(評論),但也有同樣的結果。

這是我檢索何時使用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> 

回答

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"/> 
+0

謝謝!你很棒。我不知道爲什麼我認爲默認是垂直的。 – Adrian 2011-02-24 14:01:09

+0

感謝人^。^你的回答非常有幫助。 – anticafe 2011-06-05 08:18:24

+0

非常感謝你franco – 2012-01-23 05:03:14

2

使用層次觀衆以檢查是否有真的只有1視圖中添加,或者,你可以看到只有一個視圖。例如,如果您重複此行,則此行android:layout_below="@id/post_list_item_footer_text"可能會很麻煩?我不知道該預期的行爲...