簡體   English   中英

NestedScrollView(和 ScrollView)不適用於 ConstraintLayout

[英]NestedScrollView(and ScrollView) not working with ConstraintLayout

這是我的 xml 代碼。 很簡單,但 NestedScrollView 無法正常工作。 當我將 Linearlayout 的高度屬性從 0dp 更改為 wrap_content 時,NestedScrollView 可以工作,但 ImageView 的圖像以奇怪的外觀拉伸。 我該如何解決這個問題? 順便說一句,'@string/lorem' 的字符串比屏幕大小還長。

<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="match_parent"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/sv_product"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.5" />

            <ImageView
                android:id="@+id/iv_product_image"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/holo_orange_dark"
                app:layout_constraintBottom_toBottomOf="@id/gl_image_view"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_product_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.075" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_product_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.925" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@id/gl_product_right"
                app:layout_constraintStart_toStartOf="@id/gl_product_left"
                app:layout_constraintTop_toTopOf="@id/gl_image_view">

                <TextView
                    android:id="@+id/tv_product_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:text="@string/lorem" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

這是當 Linearlayout 的高度屬性為 0dp 時

這是當 Linearlayout 的 height 屬性是 wrap_content

問題是您的線性布局約束。 LinearLayout使用app:layout_constraintTop_toBottomOf="@id/iv_product_image"app:layout_constraintTop_toBottomOf="@id/gl_image_view"
您可以根據需要通過調整gl_image_view layout_constraintGuide_percent來更改圖像大小

這是具有這些更改的工作代碼。

<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="match_parent"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/sv_product"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.3" />

            <ImageView
                android:id="@+id/iv_product_image"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/holo_orange_dark"
                app:layout_constraintBottom_toBottomOf="@id/gl_image_view"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_product_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.075" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/gl_product_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.925" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:orientation="vertical"
                app:layout_constraintTop_toBottomOf="@id/gl_image_view"
                >

                <TextView
                    android:id="@+id/tv_product_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:text="@string/lorem" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

我將layout_constraintGuide_percent從 0.5 更改為 0.3 以看起來與您想要的大小相似,您可以在gl_image_view更改它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM