开发者

LinearLayout addView doesn't work properly

开发者 https://www.devze.com 2023-02-12 23:37 出处:网络
I\'m having issues with the addView method from LinearLayout. I don\'t know why, but if I add three views only the first one is displayed. Here is the code:

I'm having issues with the addView method from LinearLayout. I don't know why, but if I add three views only the first one is displayed. Here is the code:

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);
}

I've tried with addView(comment) too, but with the same result.

This is the code of the View that I retrieve when to use the findViewById mehthod.

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

And this is the XML that I inflate:

<?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>


you do not declare the LinearLayout orientation... by default is Horizontal, try setting the orientation Vertical

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


Use the hierarchy viewer to check if there really is only 1 view added, or that you can see only one view. For instance, this line android:layout_below="@id/post_list_item_footer_text" might be troublesome if you repeat it? I don't know the expected behaviour for that...

0

精彩评论

暂无评论...
验证码 换一张
取 消