NestedScrollView(和ScrollView)不适用于ConstraintLayout

李敏荣|

这是我的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>

这是当Linearlayout的height属性为0dp时

这是Linearlayout的height属性为wrap_content时

非洲文化

问题是您的线性布局约束。用于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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

marginTop不适用于ConstraintLayout和wrap_content

LiftOnScroll 不适用于 NestedScrollView

RecyclerView滚动不适用于NestedScrollView

ScrollView不适用于SimpleAccordian视图/

Android setText不适用于scrollview

Scrollview不适用于Cardview

ScrollView 不适用于本机反应

ScrollView不适用于imageviews

RefreshIndicator 不适用于 Scrollview

分页不适用于NestedScrollView中的RecyclerView

ScrollView snapToInterval和snapToAlignment不适用于文本元素列表

fill_parent在ScrollView中不适用于RelativeLayout

间距和分层不适用于图像和列表

xsel和xclip是否仅适用于复制而不适用于粘贴?

Fullcalendar和Bootstrap:回调适用于.modal(),但不适用于.dropdown('toggle')

datepicker不适用于FF和chrome,但适用于IE

截断样式不适用于<a>标签,但适用于<div>和<p>标签

CSS @ font-face不适用于Firefox,但适用于Chrome和IE

Localizable.strings 适用于 String,但不适用于 Double 和 Integer 32

程序适用于 Ubuntu 17.04 和 17.10,但不适用于 Ubuntu 18.04

jQuery Javascript仅适用于chrome和firefox,不适用于IE

jQuery验证适用于类,但不适用于规则和消息

Kerberos授权不适用于Chrome和FireFox,但适用于IE

SVG 动画不适用于 mozilla,但适用于 chrome 和 safari

Socket.IO适用于Firefox和Edge,但不适用于Chrome吗?

自定义元素适用于Safari,但不适用于Firefox和Chrome

jQuery touch Punch适用于Chrome和Firefox,但不适用于IE

提交按钮不适用于Android,但适用于PC和iPhone

Telnet 适用于 AWS URL 和 localhost 但不适用于 IP