如何在android中插入布局到布局?

如何在android中插入布局到布局?,android,android-layout,Android,Android Layout,我有两个布局第一和第二,我想插入第二到第一。 我想将第二个布局插入到id@+id/layout的布局中 当我按下“获取布局”按钮时,它会在按钮底部显示第二个布局 第一布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andro

我有两个布局第一和第二,我想插入第二到第一。 我想将第二个布局插入到id@+id/layout的布局中 当我按下“获取布局”按钮时,它会在按钮底部显示第二个布局

第一布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
    android:id="@+id/btn_get_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Layout" />

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>
<include 
     layout="@layout/second_layout"
     android:id="@+id/includedLayout"
     android:visibility="gone"
     android:layout_below="@id/buttonId" />

第二布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/card_beauty" />

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/sample_0" />
</RelativeLayout>

</LinearLayout>


使用LayoutFlator将外部布局充气到现有布局中。在Android开发者网站上查看更多信息。

如果我理解正确,您应该在第一个布局中使用以下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
    android:id="@+id/btn_get_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Layout" />

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>
<include 
     layout="@layout/second_layout"
     android:id="@+id/includedLayout"
     android:visibility="gone"
     android:layout_below="@id/buttonId" />
使用 这里有一个更完整的例子。使用
include
将方形蓝色布局插入主布局

活动\u main.xml

这是主要布局。使用
include
引用自定义布局。您也可以覆盖此处的任何属性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- Here is the inserted layout -->
    <include layout="@layout/my_layout"/>

</RelativeLayout>

my_layout.xml

这是将插入主布局的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:text="My Layout"/>

</RelativeLayout>

您应该使用此代码段在另一个代码段中添加布局

    parentLayout=(LinearLayout)findViewById(R.id.parentLayout);
    inflateView=View.inflate(this,R.layout.view_video,parentLayout);

这里的
parentLayout
是根视图,
R.layout.View\u video
是您需要插入的布局。

如果我需要动态插入多个布局(布局的数量事先未知),我该如何做?