Java Android:动态地将编程创建的布局添加到布局中

Java Android:动态地将编程创建的布局添加到布局中,java,android,android-layout,kotlin,Java,Android,Android Layout,Kotlin,如何将动态创建的GridLayout添加到常规XMLLinearLayout?我已经找到了如何将常规XML布局添加到另一个布局的示例,但是在做了研究之后,我还没有找到关于动态创建布局的示例。下面是我的代码,我正在动态创建一个GridLayout,我想将它添加到一个id为“difference\u table”的布局中 您可以通过编程将任何组件添加到XML中的“容器”中,就像LinearLayour一样 例如: kotlin代码 <?xml version="1.0" encoding="u

如何将动态创建的
GridLayout
添加到常规XML
LinearLayout
?我已经找到了如何将常规XML布局添加到另一个布局的示例,但是在做了研究之后,我还没有找到关于动态创建布局的示例。下面是我的代码,我正在动态创建一个
GridLayout
,我想将它添加到一个id为“
difference\u table
”的布局中


您可以通过编程将任何组件添加到
XML
中的“容器”中,就像
LinearLayour
一样

例如:

kotlin代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/layDynContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
首先,您需要根据需要以编程方式创建组件(TextView、GridView、Edittext…)

var _newEditText = EditText(activity)
然后输入代码,将此组件添加到
布局容器中

val rl = findViewById(R.id.layDynContainer) as LinearLayout
        rl.addView(_newEditText)
Xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/layDynContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

现在,您可以对任何组件执行相同的操作