gpt4 book ai didi

android - LinearLayout addView 无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:32 48 4
gpt4 key购买 nike

我在使用 LinearLayout 的 addView 方法时遇到问题。我不知道为什么,但如果我添加三个 View ,则只显示第一个。这是代码:

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 方法时检索的 View 的代码。

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

最佳答案

你没有声明 LinearLayout 的方向...默认是 Horizo​​ntal,尝试设置方向 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"/>

关于android - LinearLayout addView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5104936/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com