NestedScrollView (и ScrollView) не работают с ConstraintLayout - PullRequest
1 голос
/ 18 января 2020

Вот мой 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 равно wrap_content

1 Ответ

2 голосов
/ 18 января 2020

Проблема в ограничениях линейного макета. Используйте app:layout_constraintTop_toBottomOf="@id/iv_product_image" или app:layout_constraintTop_toBottomOf="@id/gl_image_view" для LinearLayout. Размер изображения вы можете изменить в соответствии с вашими потребностями, настроив layout_constraintGuide_percent из gl_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

...