【问题标题】:NestedScrollView(and ScrollView) not working with ConstraintLayoutNestedScrollView(和 ScrollView)不适用于 ConstraintLayout
【发布时间】:2020-01-18 17:31:15
【问题描述】:

这是我的 xml 代码。这很简单,但 NestedScrollView 无法正常工作。当我将 Linearlayout 的 height 属性从 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>

This is when the height property of Linearlayout is 0dp

This is when the height property of Linearlayout is wrap_content

【问题讨论】:

    标签: android scrollview android-nestedscrollview


    【解决方案1】:

    问题是您的线性布局约束。使用app:layout_constraintTop_toBottomOf="@id/iv_product_image"app:layout_constraintTop_toBottomOf="@id/gl_image_view"LinearLayout
    您可以根据需要通过调整来更改图像大小 layout_constraintGuide_percentgl_image_view

    这是进行这些更改的工作代码。

    <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 中更改它

    【讨论】:

    • 感谢您的回答,但您回答的问题是 gl_image_view 的高度取决于 TextView(tv_product_description) 的长度。我想要修复它,还有一件事是 Linearlayout 的宽度不能应用于 gl_product_left 和 gl_product_right 的指南,如果我像这样更改它 android:layout_width= 0dp app:layout_constraintStart_toStartOf=gl_product_left, app:layout_constraintEnd_toEndOf=gl_product_right 我可能应该找到另一种方法,无论如何谢谢你的努力
    • 谢谢它对我有用
    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 2015-10-22
    • 2018-06-13
    • 1970-01-01
    • 2015-09-25
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多