使用 inflater 包含 edittext 时按钮不会下降

button not going down when using inflater to include edittext

我正在使用 inflater 从子布局 copy/include 编辑文本,但问题是保存按钮在应用 adds/includes/copies 子布局后没有下降。我想要的是,按钮位于最后一个 "edittext".

的末尾

这是我的主要布局:

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


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

    <include layout="@layout/stocker_child" />

    <Button
        android:id="@+id/save"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Simpan" />

</LinearLayout>

 </ScrollView>

这是子布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp">

<EditText
    android:id="@+id/barcodeText0"
    android:tag="barcodeText0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="17dp"
    android:layout_marginStart="16dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintBottom_toBottomOf="@+id/qtyText0"
    app:layout_constraintEnd_toStartOf="@+id/qtyText0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/qtyText0" />

<EditText
    android:id="@+id/qtyText0"
    android:tag="qtyText0"
    android:layout_width="78dp"
    android:layout_height="47dp"
    android:layout_marginEnd="12dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPhonetic|numberDecimal" android:digits="/0123456789.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/barcodeText0"
    app:layout_constraintTop_toTopOf="parent" />
 </android.support.constraint.ConstraintLayout>

这就是我膨胀子布局的方式:

                    val inflater = activity.layoutInflater
                val myLayout = inflater.inflate(R.layout.stocker_child, mainLayout, false)

                mainLayout.addView(myLayout)
                mainLayout.requestFocus()

mainLayout.addView() 会将您的观点添加为 mainLayout 中的最后一项。使用不同的方法重载:

mainLayout.addView(myLayout, mainLayout.getChildCount() - 1);

这应该在您的按钮上方添加 myLayout