使用 nestedscrollview 不会在使用 constraintlayout 的 textview 下对齐

Using nestedscrollview doesn't align under a textview using constraintlayout

提问人:ant2009 提问时间:11/13/2023 最后编辑:ant2009 更新时间:11/13/2023 访问量:23

问:

我有以下布局

enter image description here

在预览窗口中似乎还可以。

但是当我启动底表时,它与上面的视图重叠。我不确定这是否与 nestedScrollview 有关。我之所以使用 NestScrollView,是因为用户可以在首次打开时滚动底部表以将其向上移动。如果我只使用计划 ScrollView,则内容将不会滚动,并且仍然会显示不正确。

enter image description here

我不明白这个,这是我的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_bottom_sheet_sort_filter">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_bottom_sheet_sort_filter"
        android:elevation="@dimen/margin_2dp"
        android:translationZ="@dimen/margin_2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_offer_tv"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/margin_60dp"
            android:gravity="center"
            android:letterSpacing="0.03"
            tools:text="Get ฿1,000 off"
            android:textColor="@color/black"
            android:textSize="@dimen/text_large"
            app:fontFamily="@font/cpn_bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/close_icon"
            android:layout_width="@dimen/margin_22dp"
            android:layout_height="@dimen/margin_22dp"
            android:layout_marginEnd="@dimen/margin_16dp"
            android:background="@drawable/close_icon_terms"
            app:layout_constraintBottom_toBottomOf="@id/coupon_offer_tv"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/coupon_offer_tv" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/coupan_terms_and_cond_bg"
        android:paddingHorizontal="@dimen/margin_16dp"
        android:paddingTop="@dimen/margin_24dp"
        android:paddingBottom="@dimen/margin_24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cardview">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_code_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:letterSpacing="0.01"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/text_medium"
            android:textStyle="bold"
            app:fontFamily="@font/cpn_bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:textAllCaps="false"
            tools:text="This is the coupon TV Code"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_desc_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/text_small"
            app:fontFamily="@font/cpn_regular"
            app:layout_constraintStart_toStartOf="@+id/coupon_code_tv"
            app:layout_constraintTop_toBottomOf="@+id/coupon_code_tv"
            app:textAllCaps="false"
            tools:text="Coupon description that describes the coupons"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/copy_coupon_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:padding="@dimen/margin_14dp"
            tools:text="Copy"
            android:textColor="@color/white"
            android:textSize="@dimen/text_vlarge"
            app:fontFamily="@font/bebasneue_regular"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/coupon_code_tv"
            app:textAllCaps="true" />

        <androidx.core.widget.NestedScrollView
            android:layout_width="@dimen/margin_0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_14dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/copy_coupon_tv">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/terms_and_condition_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:includeFontPadding="false"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="These are the rules we must follow" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/coupon_brand_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/terms_and_condition_rules_tv"
                    tools:text="Aesop, Amazfit, Apple, Beats, Bodum, Cellreturn, Chanel, Garmin, Genedia, Gift Card, Gopro, Hatari, Lenovo, Lets Clean, Lg, Marshall, MI, Muji, Nintendo, Nt, Pasaya, Radley , Reebok, Samsung, Sharp, Sony, Suunto, Toshiba, Vivo, Xfit, Xiaomi, Zebra" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/coupon_product_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/coupon_brand_rules_tv"
                    tools:text="@tools:sample/lorem/random"/>

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

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
android-layout

评论


答:

1赞 Bilal Naeem 11/13/2023 #1

尝试将 your 的属性设置为 。此外,您可以设置要换行的默认高度,以便它只占用所需的空间。layout_heightNestedScrollViewmatch_constraint

<androidx.core.widget.NestedScrollView
    android:layout_width="@dimen/margin_0dp"
    android:layout_height="0dp" //<--- Match constraint
    android:layout_marginTop="@dimen/margin_14dp"
    android:fillViewport="true"
    app:layout_constraintHeight_default="wrap" //<-- Take only the amount of space required
    app:layout_constraintVertical_bias="0" //<-- This will force the view to align to the top
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/copy_coupon_tv">