【问题标题】:Constraint layout not matching parent in Nested scrollview约束布局与嵌套滚动视图中的父级不匹配
【发布时间】:2020-12-18 22:55:21
【问题描述】:

约束布局与 Nestedscrollview 中的父级不匹配,并且都在坐标布局中。 所以,我在坐标布局中包含布局文件。 下面是 Layout 的代码和图像。

 <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"

   >
<androidx.core.widget.NestedScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <TextView
           android:id="@+id/delivery_tv_label"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:text="Delivery"
           android:textSize="14sp"
           android:textAlignment="center"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toLeftOf="@id/rating_tv_label"
           app:layout_constraintHorizontal_weight="5"
           />
        <TextView
            android:id="@+id/rating_tv_label"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Overall Rating"
            android:textSize="14sp"
            android:textAlignment="center"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/delivery_tv_label"
            app:layout_constraintHorizontal_weight="5"
            />
        <TextView
            android:id="@+id/delivery_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="21 Mins"
            android:textSize="16sp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textAlignment="center"
            android:layout_marginTop="5dp"
           app:layout_constraintTop_toBottomOf="@id/delivery_tv_label"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/rating_tv_label"
            app:layout_constraintHorizontal_weight="5"
            />

        <LinearLayout
            android:id="@+id/linear_layout"
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            app:layout_constraintTop_toBottomOf="@id/rating_tv_label"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/delivery_tv"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:id="@+id/rating_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4.2"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:textAlignment="center"

                />

            <ImageView
                android:id="@+id/star_img"
                android:src="@drawable/ic_baseline_star_24"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginTop="2dp"

                />
        </LinearLayout>
        <TextView
            android:id="@+id/rets"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="@string/large_text"
            android:textSize="16sp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_item_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/linear_layout"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"

            />

    </androidx.constraintlayout.widget.ConstraintLayout>

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

此布局是 CoordinatorLayout 的子布局之一

【问题讨论】:

    标签: android android-layout constraints android-constraintlayout android-coordinatorlayout


    【解决方案1】:

    编辑:我将详细说明为什么这是解决方案。

    您需要fillViewPort 的原因是您有一个RecyclerView,这意味着您想用它填充可用空间,因为您也使用了0dp。但是,问题在于没有fillViewPort,它不会像填充可用空间(0dp)那样工作,而是像wrap_content一样工作。因此,在您设置fillViewPort 的那一刻,即使内容较少,它也会填满所有可用空间。检查these images 是否正常工作。

    您也可以查看类似问题here


    记住 ScrollView 子级的默认高度是 wrap_content,因此它的子级布局 ConstraintLayout 也将 wrap_content 作为高度,如下图所示的警告 IDE 所示。 NestedScrollView 的情况下不会显示此警告,但其子级仍充当wrap_content

    所以,您只需将android:fillViewport="true" 中的NestedScrollView 设置为:

    <androidx.core.widget.NestedScrollView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
        ...
    </androidx.core.widget.NestedScrollView>
    

    设置fillViewPort 可以解决问题,因为它的行为类似于match_parent,或者在您的情况下为0dp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多