的LinearLayout addView不能正常工作 [英] LinearLayout addView doesn't work properly

查看:135
本文介绍了的LinearLayout addView不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与LinearLayout中的addView方法的问题。我不知道为什么,但如果我添加三个视图只显示第一个。这里是code:

 注释[]评论=帖[位置] .getComments();
的LinearLayout布局=(的LinearLayout)convertView.findViewById(R.id.post_list_item_comments);
layout.removeAllViews();
的for(int i = 0; I< comments.length;我++){
    查看评论= 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(评论,我);
}
 

我试着addView(评论)也一样,但相同的结果。

这是我检索时使用的findViewById mehthod观为code。

 <的LinearLayout
    机器人:ID =@ + ID / post_list_item_comments
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:以下属性来=40dip
    机器人:layout_below =@ ID / post_list_item_footer_text
    机器人:能见度=水涨船高/>
 

这是我吹的XML:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <查看
        机器人:ID =@ + ID / post_list_item_comment_divider
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1dip
        机器人:背景=@可绘制/除法
        机器人:layout_marginTop =2DIP
        机器人:layout_marginBottom =2DIP/>
    < ImageView的
        机器人:ID =@ + ID / post_list_item_comment_photo
        机器人:layout_width =40dip
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ ID / post_list_item_comment_divider
        机器人:adjustViewBounds =真/>
    <的TextView
        机器人:ID =@ + ID / post_list_item_comment_name
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_toRightOf =@ ID / post_list_item_comment_photo
        机器人:layout_below =@ ID / post_list_item_comment_divider
        机器人:MAXLINES =2
        机器人:ellipsize =结束/>
    <的TextView
        机器人:ID =@ + ID / post_list_item_comment_message
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_toRightOf =@ ID / post_list_item_comment_photo
        机器人:layout_below =@ ID / post_list_item_comment_name
        机器人:TEXTSIZE =13SP
        机器人:MAXLINES =2
        机器人:ellipsize =结束/>
< / RelativeLayout的>
 

解决方案

你不申报的LinearLayout方向......在默认情况下是水平的,尝试设置方向垂直

 <的LinearLayout
    机器人:ID =@ + ID / post_list_item_comments
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:以下属性来=40dip
    机器人:layout_below =@ ID / post_list_item_footer_text
    机器人:能见度=水涨船高/>
 

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

这篇关于的LinearLayout addView不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆