Android ConstraintLayout ScrollView 不滚动
Posted
技术标签:
【中文标题】Android ConstraintLayout ScrollView 不滚动【英文标题】:Android ConstraintLayout ScrollView is not scrolling 【发布时间】:2020-04-29 08:59:54 【问题描述】:我有这个布局,但它不滚动。
我想让视图大于设备屏幕(又名 1440x3040 xxhdpi)。
所以我设置了scrollVeiw
android:layout_height="1453dp"
。 (这个数字来自 zeplin 仪表板)
如果我将scrollVeiw
设置为android:layout_height="match_parent"
,这会使视图太短。
view design image
这是我的 xml 代码
<ScrollView
android:id="@+id/scrollView2"
android:layout_
android:layout_
android:fillViewport="true"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout1"
android:layout_
android:layout_>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_name_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4068" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_email_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4782" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_pwd_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5495" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_pwd_chk_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6208" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2547" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img_change_btn_horizontal"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3284" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img_change_btn_vertical"
android:layout_
android:layout_
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5972" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_email_login_btn"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9062" />
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.2009"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="1.0"
app:srcCompat="@color/colorAccent" />
<EditText
android:id="@+id/name_text"
android:layout_
android:layout_
android:drawableLeft="@mipmap/name"
android:ems="10"
android:inputType="textPersonName"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_name_text"
app:layout_constraintWidth_percent="0.8" />
<EditText
android:id="@+id/email_text"
android:layout_
android:layout_
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_email_text"
app:layout_constraintWidth_percent="0.8" />
<EditText
android:id="@+id/pwd_text"
android:layout_
android:layout_
android:ems="10"
android:inputType="textPassword"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_pwd_text"
app:layout_constraintWidth_percent="0.8"
/>
<EditText
android:id="@+id/pwd_chk_text"
android:layout_
android:layout_
android:drawableLeft="@mipmap/name"
android:ems="10"
android:inputType="textPassword"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_pwd_chk_text"
app:layout_constraintWidth_percent="0.8" />
<ImageButton
android:id="@+id/email_login_button"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.0474"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_email_login_btn"
app:layout_constraintWidth_percent="0.5568"
app:srcCompat="@mipmap/signin_btn" />
<ImageView
android:id="@+id/member_img"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1028"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_member_img"
app:layout_constraintWidth_percent="0.3806"
app:srcCompat="@mipmap/person" />
<ImageButton
android:id="@+id/member_img_change_btn"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintHeight_percent="0.0282"
app:layout_constraintStart_toStartOf="@+id/guideline_member_img_change_btn_vertical"
app:layout_constraintTop_toTopOf="@+id/guideline_member_img_change_btn_horizontal"
app:layout_constraintWidth_percent="0.3068"
app:srcCompat="@mipmap/change_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
【问题讨论】:
【参考方案1】:解决方案:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout1"
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_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ScrollView
android:id="@+id/scrollView2"
android:layout_
android:layout_
android:fillViewport="true"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_name_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4068" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_email_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4782" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_pwd_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5495" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_pwd_chk_text"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6208" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2547" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img_change_btn_horizontal"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3284" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_member_img_change_btn_vertical"
android:layout_
android:layout_
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5972" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_email_login_btn"
android:layout_
android:layout_
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9062" />
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.2009"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="1.0"
app:srcCompat="@color/colorAccent" />
<EditText
android:id="@+id/name_text"
android:layout_
android:layout_
android:drawableLeft="@mipmap/name"
android:ems="10"
android:inputType="textPersonName"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_name_text"
app:layout_constraintWidth_percent="0.8" />
<EditText
android:id="@+id/email_text"
android:layout_
android:layout_
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_email_text"
app:layout_constraintWidth_percent="0.8" />
<EditText
android:id="@+id/pwd_text"
android:layout_
android:layout_
android:ems="10"
android:inputType="textPassword"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_pwd_text"
app:layout_constraintWidth_percent="0.8"
/>
<EditText
android:id="@+id/pwd_chk_text"
android:layout_
android:layout_
android:drawableLeft="@mipmap/name"
android:ems="10"
android:inputType="textPassword"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.04"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_pwd_chk_text"
app:layout_constraintWidth_percent="0.8" />
<ImageButton
android:id="@+id/email_login_button"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.0474"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_email_login_btn"
app:layout_constraintWidth_percent="0.5568"
app:srcCompat="@mipmap/signin_btn" />
<ImageView
android:id="@+id/member_img"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1028"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_member_img"
app:layout_constraintWidth_percent="0.3806"
app:srcCompat="@mipmap/person" />
<ImageButton
android:id="@+id/member_img_change_btn"
android:layout_
android:layout_
android:ems="10"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintHeight_percent="0.0282"
app:layout_constraintStart_toStartOf="@+id/guideline_member_img_change_btn_vertical"
app:layout_constraintTop_toTopOf="@+id/guideline_member_img_change_btn_horizontal"
app:layout_constraintWidth_percent="0.3068"
app:srcCompat="@mipmap/change_image" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
【讨论】:
【参考方案2】:为ConstraintLayout设置固定高度(1453dp
),并使ScrollView的高度为match_parent
:
<ScrollView
android:id="@+id/scrollView2"
android:layout_
android:layout_
android:fillViewport="true"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout1"
android:layout_
android:layout_>
...
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
【讨论】:
【参考方案3】:尝试使用 NestedScrollView 而不是滚动视图
<android.support.v4.widget.NestedScrollView
android:layout_
android:layout_
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_
android:layout_>
</android.support.constraint.ConstraintLayout>
【讨论】:
以上是关于Android ConstraintLayout ScrollView 不滚动的主要内容,如果未能解决你的问题,请参考以下文章
Android开发 - 掌握ConstraintLayout介绍
是否建议在 Android 的 ConstraintLayout 中使用 LinearLayout?