以编程方式嵌套布局

时间:2018-04-04 08:03:26

标签: java android android-layout

我已经使用代码将一个布局添加到另一个布局,但在这种特殊情况下,我无法让它为我工作。如果在xml文件中使用include添加它们,它可以正常工作,它会尝试在代码中复制.xml文件的包含。布局如下:

父布局(parent.xml):

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/activity_horizontal_margin"
        android:orientation="vertical">

        <!--<include layout="@layout/child_1" /> this work fine -->

    </LinearLayout>

</ScrollView>

第一个孩子(child_1.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/child_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/media_card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="75dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/child_1_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

            <!-- First row: header -->
            <LinearLayout
                android:id="@+id/header_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    ... />

                <TextView
                    ... />

            </LinearLayout>

            <!-- Second row: ... -->
            <!-- <include layout="@layout/child_2" />  this work fine  -->
            include child_2 programmatically here. This does not work, 

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

第二个孩子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/child_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        ... />

</LinearLayout>

我尝试复制之前的java代码包括:

LinearLayout parent = (LinearLayout) lyInflater.inflate(R.layout.parent, (ViewGroup) findViewById(R.id.parent));

LinearLayout child_1 = (LinearLayout) lyInflater.inflate(R.layout.child_1, (ViewGroup) findViewById(R.id.child_1));

LinearLayout child_1_2 = (LinearLayout) lyInflater.inflate(R.layout.child_1, (ViewGroup) findViewById(R.id.child_1_2));

LinearLayout child_2 = (LinearLayout) lyInflater.inflate(R.layout.child_2, (ViewGroup) findViewById(R.id.child_2));

child_1_2.addView(child_2);
parent.addView(child_1);

父级布局正确添加到child_1,但child_1未显示child_2。

1 个答案:

答案 0 :(得分:0)

您需要在此处展示1个布局: child_2

child_1_2 应根据 child_1 定义

LinearLayout child_1_2 = child_1.findViewById(R.id.child_1_2);