用于ConstraintLayout的求解器无法正确测量包含

时间:2019-06-27 03:40:50

标签: android android-constraintlayout

我目前正在UI中练习Android设计,需要使一个布局作为标题包含在多个布局中。所以我在下面做了这个相当复杂的布局。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f5f5f5">

    <include
        android:id="@+id/includeItem"
        layout="@layout/dialog_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginTop="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/includeItem">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.constraint.Guideline
                android:id="@+id/guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="16dp" />

            <android.support.constraint.Guideline
                android:id="@+id/guideline2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_end="16dp" />

            <EditText
                android:id="@+id/editText4"
                style="@style/CustomEditText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="16dp"
                android:ems="10"
                android:hint="@string/name"
                android:inputType="textPersonName"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/imageView5"
                app:layout_constraintTop_toTopOf="@+id/imageView5" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="32dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@color/btn_bg" />

            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:background="@drawable/border_transparent_bg"
                android:drawableRight="@drawable/ic_chevron_right_black_24dp"
                android:gravity="start|center_vertical"
                android:padding="8dp"
                android:text="@string/select_category"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="@+id/editText4"
                app:layout_constraintStart_toStartOf="@+id/editText4"
                app:layout_constraintTop_toBottomOf="@+id/editText4" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="@string/tap_to_edit"
                android:textColor="@color/textDarkSecondary"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="@+id/imageView5"
                app:layout_constraintStart_toStartOf="@+id/imageView5"
                app:layout_constraintTop_toBottomOf="@+id/imageView5" />

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/border_transparent_bg"
                android:padding="8dp"
                app:layout_constraintEnd_toEndOf="@+id/tableRow3"
                app:layout_constraintStart_toStartOf="@+id/tableRow3"
                app:layout_constraintTop_toBottomOf="@+id/tableRow3">

                <EditText
                    android:id="@+id/editText7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="4dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:ems="10"
                    android:hint="@string/price"
                    android:inputType="textPersonName"
                    android:padding="8dp"
                    android:textSize="24sp" />

                <EditText
                    android:id="@+id/editText6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="4dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:ems="10"
                    android:hint="@string/sku"
                    android:inputType="textPersonName"
                    android:padding="8dp"
                    android:textSize="24sp" />
            </TableRow>

            <TextView
                android:id="@+id/textView11"
                style="@style/ModuleTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:text="@string/variations"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView10" />

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/border_transparent_bg"
                android:padding="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/textView11"
                app:layout_constraintTop_toBottomOf="@+id/textView11">

                <EditText
                    android:id="@+id/editText8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:ems="10"
                    android:hint="@string/name"
                    android:inputType="textPersonName"
                    android:padding="8dp"
                    android:textSize="24sp" />
            </TableRow>

            <Button
                android:id="@+id/button6"
                style="@style/CommonButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/add_variation"
                app:layout_constraintEnd_toEndOf="@+id/tableRow4"
                app:layout_constraintStart_toStartOf="@+id/tableRow4"
                app:layout_constraintTop_toBottomOf="@+id/tableRow4" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="@string/add_variation_msg"
                android:textColor="@color/textDarkSecondary"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="@+id/button6"
                app:layout_constraintStart_toStartOf="@+id/button6"
                app:layout_constraintTop_toBottomOf="@+id/button6" />

            <TextView
                android:id="@+id/textView13"
                style="@style/ModuleTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:text="@string/modifier_sets"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView12" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toStartOf="@+id/guideline2"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/textView13">

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

            <TextView
                android:id="@+id/textView14"
                style="@style/ModuleTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:text="@string/taxes"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView2"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_marginTop="16dp"
                app:layout_constraintEnd_toStartOf="@+id/guideline2"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/textView14" />

            <TextView
                android:id="@+id/textView17"
                style="@style/ModuleTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:text="@string/select_kitchen_location"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/recyclerView2" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:padding="8dp"
                app:layout_constraintEnd_toStartOf="@+id/guideline2"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/textView17" />

            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/available"
                android:textColor="@color/black"
                android:textSize="24sp"
                app:layout_constraintEnd_toStartOf="@+id/guideline2"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/spinner" />

            <EditText
                style="@style/CustomEditText"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:textColor="@color/black"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline2"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/checkBox" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

这是包含布局。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgDlgHeaderAction"
        android:layout_width="90dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/lblDlgHeaderTitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_add_24dp" />

    <Button
        android:id="@+id/btnDlgHeaderSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_bg_pressed"
        android:paddingStart="32dp"
        android:paddingTop="16dp"
        android:paddingEnd="32dp"
        android:paddingBottom="16dp"
        android:text="@string/save"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lblDlgHeaderTitle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/btnDlgHeaderSave"
        app:layout_constraintEnd_toStartOf="@+id/btnDlgHeaderSave"
        app:layout_constraintStart_toEndOf="@id/imgDlgHeaderAction"
        app:layout_constraintTop_toTopOf="@+id/btnDlgHeaderSave" />
</android.support.constraint.ConstraintLayout>

在布局编辑器中,看起来不错。但是,当我编译我的代码时,ScrollView正在切入include,而没有那么多的marginTop。如果这是一个错误,我可能会提出一个问题。

根据以下建议,我尝试将ScrollView更改为NestedScrollView。如我所料,它仍然无法正常工作。

我还想强调一点,我的问题不是关于RecyclerView无法滚动的问题,因为我知道那会发生,而是关于ScrollView到{{1 }}布局。

1 个答案:

答案 0 :(得分:0)

  

如下更改您的包含布局约束

     

删除底部constarint,并在主布局中使用NestedScrollView而不是ScrollView

enter image description here

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

    <ImageView
        android:id="@+id/imgDlgHeaderAction"
        android:layout_width="90dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/btnDlgHeaderSave"
        app:layout_constraintEnd_toStartOf="@+id/lblDlgHeaderTitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnDlgHeaderSave"
        app:srcCompat="@drawable/ic_email" />

    <Button
        android:id="@+id/btnDlgHeaderSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@color/bg_toolbar"
        android:gravity="center"
        android:paddingStart="32dp"
        android:paddingTop="16dp"
        android:paddingEnd="32dp"
        android:paddingBottom="16dp"
        android:text="@string/save"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/lblDlgHeaderTitle"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lblDlgHeaderTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/btnDlgHeaderSave"
        app:layout_constraintEnd_toStartOf="@+id/btnDlgHeaderSave"
        app:layout_constraintStart_toEndOf="@+id/imgDlgHeaderAction"
        app:layout_constraintTop_toTopOf="@+id/btnDlgHeaderSave" />
</androidx.constraintlayout.widget.ConstraintLayout>
  

主要布局

 <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"
    android:background="#f5f5f5">

    <include
        android:id="@+id/includeItem"
        layout="@layout/test_include"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/includeItem">

       <!.. Remining one as it is>

     </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>