如何在键盘出现在ConstraintLayout中时向上移动视图

时间:2016-09-30 12:46:32

标签: android xml android-constraintlayout

现在,我在Button XML文件中有一个EditText和一个ConstraintLayout视图。用户在EditText内输入时需要移动视图。

清单windowSoftInputMode不起作用,所以我认为我需要处理约束。有没有人知道如何解决这个问题?

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/sx_landing_bg"
        android:id="@+id/fpp_background"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        tools:ignore="ContentDescription"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="1.0" />


    <android.support.v7.widget.AppCompatButton
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:text="@string/forgot_password_reset_password"
        android:id="@+id/btn_login"
        android:textColor="@color/colorDarkGray"
        android:theme="@style/WhiteButtonStyle"
        android:backgroundTint="@color/colorPrimaryDark"
        android:textAllCaps="false"
        app:layout_constraintLeft_toLeftOf="@+id/gl_vl_16d"
        app:layout_constraintRight_toLeftOf="@+id/gl_vr_16d"
        app:layout_constraintBottom_toTopOf="@+id/gl_vh_80p"
        app:layout_constraintTop_toTopOf="@+id/gl_vh_70p"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        android:gravity="center_vertical|center_horizontal|center" />

    <android.support.constraint.Guideline
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:id="@+id/gl_vh_80p"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="541dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.7922078"
        />

    <android.support.constraint.Guideline
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:id="@+id/gl_vh_70p"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="478dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.7"
        />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="683dp"
        android:id="@+id/gl_vr_16d"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="395dp"
        app:layout_constraintGuide_end="16dp"
        />

    <android.support.constraint.Guideline
        android:layout_width="0dp"
        android:layout_height="683dp"
        android:id="@+id/gl_vl_16d"
        app:layout_constraintGuide_begin="16dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="16dp"
        />

    <android.support.constraint.Guideline
        android:layout_width="0dp"
        android:layout_height="683dp"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_begin="387dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="387dp" />

    <android.support.constraint.Guideline
        android:layout_width="0dp"
        android:layout_height="683dp"
        android:id="@+id/guideline4"
        app:layout_constraintGuide_begin="24dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="24dp" />

    <android.support.constraint.Guideline
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:id="@+id/guideline9"
        app:layout_constraintGuide_begin="372dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="372dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        app:layout_constraintGuide_begin="431dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="431dp"
        tools:layout_editor_absoluteX="0dp" />

    <EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/editText2"
        android:textColor="@color/button_text_color"
        android:theme="@style/WhiteButtonStyle"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline9"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="1.0"
        android:hint="Enter email or ussername"
        style="@style/RobotoEditTextStyle"
        android:textSize="14sp"

        app:layout_constraintRight_toLeftOf="@+id/gl_vr_16d"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        android:layout_marginBottom="8dp"
        android:textColorHint="#bababa" />


</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

通过在运行时以编程方式设置视图并检查键盘何时出现来完成。

   ConstraintLayout.LayoutParams constraint = (ConstraintLayout.LayoutParams)login.getLayoutParams();
    constraint.topToBottom = shrinked ? R.id.gl_h_850p: R.id.gl_h_550p;
    constraint.bottomToTop = shrinked ? R.id.gl_h_999p: R.id.gl_h_650p;
    login.setLayoutParams(constraint);