简体   繁体   English

以编程方式在(线性)布局内(即ScrollView内)添加Views

[英]Programmatically add Views inside a (Linear)Layout (that is inside a ScrollView)

I have an app that, after some clicking, shows activity with news contents. 我有一个应用程序,单击一下即可显示带有新闻内容的活动。 I want at a bottom of that to show comments, which are dynamically loaded in async task. 我想在其底部显示注释,这些注释在异步任务中动态加载。

One approach is to use ListView and custom ArrayAdapter, but, i would have to put ListView inside a ScrollView, and it is is a problem, even if i manually override ListView's height. 一种方法是使用ListView和自定义ArrayAdapter,但是,即使我手动覆盖ListView的高度,我也必须将ListView放在ScrollView中,这是一个问题。 It shows one list item, and one part of the following one. 它显示一个列表项,以及下一个列表项的一部分。

The other approach is to define LinearLayout as a holder for comments, than inflate another LinearLayouts defined in it's own xml file, populate it's contents, attach to holder's view. 另一种方法是将LinearLayout定义为注释的持有人,而不是膨胀在其自己的xml文件中定义的另一个LinearLayouts,填充其内容,并附加到持有人的视图。 It works fine, from program's point of view, except it pushes comments below contents of the news. 从程序的角度来看,它可以正常工作,只是将注释添加到新闻内容下方。 It's like, it creates another scrollable view of comments underneath contents od news (contents overlap it). 就像,它在新闻内容(内容重叠)下创建了另一个可滚动的评论视图。

Is there any other way of doing this that has nothing to do with lists, or how can i make the other approach to work. 还有其他与列表无关的方法吗,或者我该如何使其他方法起作用。

The relevant code snippet from xml layout that holds news contents is: 包含新闻内容的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>

and code that corresponds to one comment is: 与一个注释相对应的代码是:

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

Thank you very much for any reply. 非常感谢您的答复。 This is quite important for me. 这对我来说很重要。

I am not very sure how you inflate your comments , but here is how I would do it. 我不太确定您如何增加您的comments ,但是这就是我的做法。

Declare the comments Layout as a LinearLayout and give it an id, and then get a reference to that LinearLayout : 将注释Layout声明为LinearLayout并为其指定一个ID,然后获取对该LinearLayout的引用:

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

and then just add child Views to this layout : 然后将子Views添加到此布局中:

commentsLayout.addView(newComment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM