Как вставить макет в макет в Android?

У меня есть два макета первый и второй, я хочу вставить второй в первый. Я хочу вставить второй макет в макет, который имеет id @+ id/layout когда я нажимаю кнопку Get 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>

второй макет

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

Ответ 1

    LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
    getLayoutInflater().inflate(R.layout.second_layout, placeHolder);

Ответ 2

Если я правильно понял, вы должны использовать следующий код в своем первом макете

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

а затем в действии вашей кнопки вы просто используете

((RelativeLayout)findViewById(R.id.includedLayout)).setVisibility(View.VISIBLE);

Ответ 3

Используйте include

Вот несколько более полный пример. Квадратный синий макет вставляется в основной макет с помощью include.

введите описание изображения здесь

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

Ответ 4

Используйте LayoutInflator для раздувания внешнего макета в существующий макет. Оформить заказ LayoutInflater на сайте Android Dev для получения дополнительной информации.

Ответ 5

Вы должны использовать этот фрагмент кода, чтобы добавить макет внутри другого.

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

Здесь parentLayout является корневым представлением и R.layout.view_video - это макет, который нужно вставить.