以编程方式在(线性)布局(位于ScrollView内)中添加视图

时间:2012-03-28 13:48:25

标签: android listview android-layout scrollview layout-inflater

我有一个应用程序,在点击一下后,显示有新闻内容的活动。我想在底部显示注释,这些注释是在异步任务中动态加载的。

一种方法是使用ListView和自定义ArrayAdapter,但是,我必须将ListView放在ScrollView中,这是一个问题,即使我手动覆盖ListView的高度。它显示了一个列表项,以及下一个列表项的一部分。

另一种方法是将LinearLayout定义为注释的持有者,而不是膨胀在其自己的xml文件中定义的另一个LinearLayouts,填充它的内容,附加到持有者的视图。从程序的角度来看,它工作得很好,除了它将评论推到新闻内容之下。就像,它在内容od news(内容重叠)下创建了另一个可滚动的评论视图。

有没有其他方法可以与列表无关,或者如何让其他方法工作。

来自xml布局的相关代码片段包含新闻内容:

<LinearLayout> //global layout
    <LinearLayout>
        //views that hold title, date, content
    </LinearLayout>      

        <LinearLayout 
           android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/commentsHolder"
            android:orientation="vertical">

        <View 
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#ed1b24"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="KOMENTARI"
            android:layout_marginLeft="5dp"/>
        <View 
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#ed1b24"/>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:orientation="vertical"
            android:id="@+id/comments_holder_view"
            android:layout_weight="1">
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

与一条评论对应的代码是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comments_holder_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">

<TextView 
    android:id="@+id/comment_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/comments_back_details"
    android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/comment_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/categoryStyle"
        />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" | "
        style="@style/categoryStyle"
        />

    <TextView 
        android:id="@+id/comment_pubdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/categoryStyle"/>

</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:orientation="horizontal"
    android:layout_margin="5dp"
    android:background="#d1d1d1">
</LinearLayout>

非常感谢您的回复。这对我来说非常重要。

1 个答案:

答案 0 :(得分:2)

我不太确定你是如何给comments充气的,但我会这样做。

将评论Layout声明为LinearLayout并为其提供ID,然后获取对LinearLayout的引用:

LinearLayout commentsLayout=(LinearLayout)findViewById(R.id.commentsLayoutId);

然后只需将子Views添加到此布局:

commentsLayout.addView(newComment);
相关问题