【问题标题】:ScrollView doesn't work properly滚动视图无法正常工作
【发布时间】:2017-07-31 21:22:52
【问题描述】:

我有两个 XML 布局文件,每个都有一个 ScrollView 作为父级。 当一个工作正常时,另一个不能正常工作(每个 XML 具有相同的层次结构)。

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:id="@+id/login_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/logo"
        android:textSize="50sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

    <TextView
        android:id="@+id/tvRegisterLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginStart="3dp"
        android:text="@string/btn_login"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/tvRegisterLabel">

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/et_email_hint"
            android:inputType="textEmailAddress"
            android:maxLength="70"
            android:maxLines="1"
            android:minLines="1"
            android:text="akarki@yahoo.com"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.15" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.85" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">

        <EditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/et_password_hint"
            android:inputType="textPassword"
            android:maxLength="30"
            android:maxLines="1"
            android:minLines="1"
            android:text="1234qwer"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout7" />

    <Button
        android:id="@+id/btnLogIn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/round_button"
        android:text="@string/btn_login"
        android:textColor="@color/colorWhite"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/btnFacebook"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:paddingBottom="15dp"
        android:paddingLeft="15dp"
        android:paddingTop="15dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/btnLogIn" />

    <ImageButton
        android:id="@+id/btnGoogle"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/google_round_button"
        android:scaleType="centerInside"
        android:src="@drawable/google_button_background"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/btnFacebook" />

    <TextView
        android:id="@+id/tvRegister"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:maxLines="2"
        android:text="@string/tv_create_account"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/tvForgotPassword" />

    <TextView
        android:id="@+id/tvForgotPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/tv_forgot_password"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        app:layout_constraintRight_toLeftOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="@+id/btnGoogle" />
</android.support.constraint.ConstraintLayout>

activity_register

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.15" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.85" />

    <TextView
        android:id="@+id/tvLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/logo"
        android:textSize="50sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvRegisterLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginStart="3dp"
        android:fontFamily="sans-serif"
        android:text="@string/label_register"
        android:textAllCaps="true"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/tvRegisterLabel">

        <EditText
            android:id="@+id/etEmailReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:hint="@string/et_email_hint"
            android:inputType="textEmailAddress"
            android:maxLength="70"
            android:maxLines="1"
            android:minLines="1"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout">

        <EditText
            android:id="@+id/etNameReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:hint="@string/et_name_hint"
            android:inputType="textCapWords"
            android:maxLength="30"
            android:maxLines="1"
            android:minLines="1"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">

        <EditText
            android:id="@+id/etPasswordReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:hint="@string/et_password_hint"
            android:inputType="textPassword"
            android:maxLength="30"
            android:maxLines="1"
            android:minLines="1"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout4">

        <EditText
            android:id="@+id/etPasswordConfirmReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:hint="@string/et_password_confirm_hint"
            android:inputType="textPassword"
            android:maxLength="30"
            android:maxLines="1"
            android:minLines="1"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout5">

        <EditText
            android:id="@+id/etPhoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:hint="@string/et_phone_hint"
            android:inputType="phone"
            android:maxLength="13"
            android:maxLines="1"
            android:minLines="1"
            android:textSize="@dimen/et_hint_size" />
    </android.support.design.widget.TextInputLayout>

    <TextView
        android:id="@+id/tvRequired"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/label_required"
        android:textAlignment="textStart"
        android:textSize="12sp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout6" />

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:background="@drawable/round_button"
        android:text="@string/label_register"
        android:textColor="@android:color/white"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/tvRequired" />
</android.support.constraint.ConstraintLayout>

所以,为了澄清我的问题:在 activity_login 中,滚动视图按预期正常工作。在 activity_register 中,滚动视图没有滚动到底部,我不知道为什么,因为我使用了与 activity_login 中相同的层次结构。 p>

我在 SO 中搜索过同样的问题,但没有找到任何解决我的问题的方法。
任何帮助将不胜感激,我正在寻找几天的解决方案。

【问题讨论】:

  • 抱歉,我没有看到 Scrollview 关闭标签
  • 对不起,我的错。无意中我在复制之前没有标记它。但是项目中有没有。

标签: android xml scrollview


【解决方案1】:

我不确定为什么这是一个问题,但问题似乎是你的水平Guideline。如果我删除Guideline,删除tvLogo对它的引用,并更改tvRegisterLabelapp:layout_constraintTop_toBottomOf="@+id/tvLogo"的引用,一切正常。

我怀疑这是因为您的布局中似乎有一套循环规则。 ConstraintLayout 的高度是wrap_content,所以它必须布局所有视图才能知道它有多大。要布置它的观点,它必须布置tvLogo。要布局tvLogo,它必须布局guideline。要布置guidelineConstraintLayout 必须知道它有多高。以此类推。

顺便说一句,您似乎可以将这个 ConstraintLayout 替换为 LinearLayout

【讨论】:

    【解决方案2】:

    抱歉没有太多的声誉可以添加评论所以给出答案,

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">
    

    activity_register xml中的这段代码中删除padding,它会解决你的问题

    【讨论】:

    • 我尝试使用您的代码,问题仍然存在 "android.support.constraint.ConstraintLayout" ,当您在 ScrollView 的位置使用此约束布局作为主布局并且在其中使用 ScrollView 时,只有它会要去工作,我也尝试了在我的项目中也不起作用的activity_login
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多