【问题标题】:LinearLayout addView doesn't work properlyLinearLayout addView 无法正常工作
【发布时间】:2011-02-24 12:53:59
【问题描述】:

我在使用 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>

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    你没有声明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"/>
    

    【讨论】:

    • 谢谢!你是对的。我不知道为什么我认为默认是垂直的。
    【解决方案2】:

    使用层次结构查看器检查是否真的只添加了 1 个视图,或者您只能看到一个视图。例如,这行android:layout_below="@id/post_list_item_footer_text" 重复一遍会不会很麻烦?我不知道预期的行为......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2023-03-26
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多