Android 如何通过添加另一个视图以编程方式添加到XML?

Android 如何通过添加另一个视图以编程方式添加到XML?,android,xml,programmatically-created,Android,Xml,Programmatically Created,我正在制作一个类似Reddit/YouTube/Yahoo News的应用程序,它使用API收集最近帖子的信息 启动应用程序时,它会显示所有最近的帖子,如下所示: <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout andr

我正在制作一个类似Reddit/YouTube/Yahoo News的应用程序,它使用API收集最近帖子的信息

启动应用程序时,它会显示所有最近的帖子,如下所示:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lesson 1"
        android:textAllCaps="false"
        android:textColor="#8c8c8c"
        android:textSize="40dp"
        android:gravity="center_horizontal"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:background="@drawable/lesson1"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="10 Comments"
        android:textColor="#000000"/>

    </LinearLayout>

目标是使所有帖子符合匹配的父级宽度和换行内容高度(最大300 dp高度),就像上面链接的图片一样,使所有帖子符合一个外观;标题在顶部,身体/图片在中间和下面的评论数量。

假设我的通用视图布局如下所示:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lesson 1"
        android:textAllCaps="false"
        android:textColor="#8c8c8c"
        android:textSize="40dp"
        android:gravity="center_horizontal"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:background="@drawable/lesson1"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="10 Comments"
        android:textColor="#000000"/>

    </LinearLayout>


每次有新帖子时,我都希望复制这种布局样式(文本、图像、文本),并将其添加到现有的XML中。我该怎么做呢?

您可以使用LayoutFlater将xml文件膨胀到视图中。然后,您可以将该视图添加到布局的任何位置。

这可以使用列表视图或回收器视图以及自定义适配器来完成