ScrollView无法正常工作

时间:2017-07-31 21:22:53

标签: android xml scrollview

我有两个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 中,scrollview正常工作,正如预期的那样。在 activity_register 中,滚动视图不会滚动到底部,我不知道为什么,因为我使用了与 activity_login 中相同的层次。

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

2 个答案:

答案 0 :(得分:0)

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

我怀疑这是因为你的布局中似乎有一套循环规则。 ConstraintLayout的高度为wrap_content,因此必须列出其所有视图以了解它的大小。要阐明其观点,必须列出tvLogo。要列出tvLogo,必须列出guideline。要列出guidelineConstraintLayout必须知道它有多高。等等。

顺便说一下,您似乎可以将此ConstraintLayout替换为LinearLayout

答案 1 :(得分:0)

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

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

padding xml中的此代码中删除activity_register,它将解决您的问题