软键盘上的可滚动屏幕打开,使用约束布局

Scrollable screen on soft keyboard open, using Constraint layout

我们如何在软键盘打开时使屏幕可滚动,同时使用约束布局。它可以通过清单中的标志 'adjustResize' 来实现,同时在 Scrollview 中使用线性布局。但是在 Scrollview 中使用 Constraint 布局时行为是不同的。 我们如何在使用约束布局时实现所需的行为?

下图分别是软键盘未打开和键盘打开时我的画面。

这是我未打开软键盘时的屏幕

这是我打开软键盘后的画面

几天以来我一直在寻找解决方案,但我找不到。 请帮忙!

编辑:

我目前将我的 XML 建模为:

 <Constraint layout>
             <NestedScrollView>
                         <Constraint layout>
                         </Constraint layout>
            </NestedScrollView>
    
            <Constraint layout>
            </Constraint layout>
    
</Constraint layout>

没用。

编辑 2:

我的实际XML是:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/dark_blue"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:isScrollContainer="true"
        android:fillViewport="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/clProgressSignUp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SignupActivity">
    <ImageView
        android:id="@+id/ivLogo"
        android:layout_width="@dimen/_148sdp"
        android:layout_height="@dimen/_60sdp"
        android:src="@drawable/logo_voodlee"
        app:layout_constraintVertical_bias="0.08"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name as on Pan Card"
                android:visibility="visible"
                app:fontFamily="@font/poppins_regular"
                android:textColor="@color/fade_green"
                android:textSize="@dimen/_11ssp"
                app:layout_constraintVertical_bias="0.285"
                app:layout_constraintHorizontal_bias="0.135"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <EditText
                android:id="@+id/etName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name as on Pan Card"
                android:gravity="left"
                android:inputType="textVisiblePassword"
                android:background="@drawable/bg_edittext_thin_line_bright_green"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:lineSpacingExtra="@dimen/_3sdp"
                app:fontFamily="@font/poppins_regular"
                android:textColor="@color/bright_green"
                android:textColorHint="@color/fade_green"
                android:textSize="@dimen/dimen_20dp"
                android:importantForAutofill="no"
                app:layout_constraintVertical_bias="0.31"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>


            <TextView
                android:id="@+id/tvEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email"
                android:visibility="visible"
                app:fontFamily="@font/poppins_regular"
                android:textColor="@color/fade_green"
                android:textSize="@dimen/_11ssp"
                app:layout_constraintVertical_bias="0.4"
                app:layout_constraintHorizontal_bias="0.091"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <EditText
                android:id="@+id/etEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textVisiblePassword"
                android:hint="Email"
                android:gravity="left"
                android:background="@drawable/bg_edittext_thin_line_bright_green"
                android:lineSpacingExtra="@dimen/_3sdp"
                app:fontFamily="@font/poppins_regular"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:textColor="@color/bright_green"
                android:textColorHint="@color/fade_green"
                android:textSize="@dimen/dimen_20dp"
                android:importantForAutofill="no"
                app:layout_constraintVertical_bias="0.43"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <TextView
                android:id="@+id/tvErrorMsgEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                android:text="* Please enter valid email address"
                android:textSize="@dimen/_10ssp"
                android:textColor="@color/voodlee_red"
                app:fontFamily="@font/poppins_regular"
                android:layout_marginTop="@dimen/_4sdp"
                app:layout_constraintHorizontal_bias=".1375"
                app:layout_constraintVertical_bias="0.47"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />

            <TextView
                android:id="@+id/tvMob"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mobile Number"
                android:gravity="center_vertical"
                android:visibility="visible"
                app:fontFamily="@font/poppins_regular"
                android:textColor="@color/fade_green"
                android:textSize="@dimen/_11ssp"
                app:layout_constraintVertical_bias="0.515"
                app:layout_constraintHorizontal_bias="0.115"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <EditText
                android:id="@+id/etMob"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:hint="Mobile Number"
                android:gravity="left"
                android:lineSpacingExtra="@dimen/_3sdp"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:textColor="@color/bright_green"
                android:textColorHint="@color/fade_green"
                android:textSize="@dimen/dimen_20dp"
                app:fontFamily="@font/poppins_regular"
                android:background="@drawable/bg_edittext_thin_line_bright_green"
                android:importantForAutofill="no"
                app:layout_constraintVertical_bias="0.55"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />

            <TextView
                android:id="@+id/tvErrorMsgMobile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                android:text="* Please enter valid mobile number"
                android:textSize="@dimen/_10ssp"
                android:textColor="@color/voodlee_red"
                app:fontFamily="@font/poppins_regular"
                android:layout_marginTop="@dimen/_4sdp"
                app:layout_constraintHorizontal_bias=".135"
                app:layout_constraintVertical_bias="0.5875"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"

                />

            <CheckBox
                android:id="@+id/cbTnc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/bright_green"
                app:layout_constraintHorizontal_bias="0.08"
                app:layout_constraintVertical_bias="0.65"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <TextView
                android:id="@+id/tvAgree"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="I agree to the"
                android:textSize="@dimen/dimen_14dp"
                android:textColor="@color/bright_green"
                app:fontFamily="@font/poppins_regular"
                app:layout_constraintHorizontal_bias="0.21"
                app:layout_constraintVertical_bias="0.65"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />

            <TextView
                android:id="@+id/tvTnc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="terms and conditions"
                android:textColor="@color/text_highlight"
                android:textSize="@dimen/dimen_14dp"
                app:fontFamily="@font/poppins_regular"
                app:layout_constraintHorizontal_bias="0.1"
                app:layout_constraintVertical_bias="0.65"
                app:layout_constraintLeft_toRightOf="@id/tvAgree"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />

            
    <Button
        android:id="@+id/btnNextSignUp"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_36sdp"
        android:background="@drawable/bg_btn_proceed_enabled"
        android:layout_margin="@dimen/_25sdp"
        android:text="Proceed"
        android:textColor="@color/dark_blue"
        android:textAllCaps="false"
        android:textSize="@dimen/dimen_20dp"
        android:onClick="onClickNext"
        android:fontFamily="@font/poppins_semi_bold"
        android:foreground="?attr/selectableItemBackground"
        app:layout_constraintVertical_bias="0.825"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


    <TextView
        android:id="@+id/tvAlreadyHave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/text_highlight"
        android:text="Already have an account?"
        app:fontFamily="@font/poppins_regular"
        android:textSize="@dimen/dimen_15dp"
        android:textStyle="bold"
        app:layout_constraintVertical_bias="0.87"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
        
    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

            <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clProgressSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:layout_constraintVertical_bias="0.96"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">
                .
                .
                .
                .
                .
                .
                .
                .

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

您需要像这样对您的 .xml 文件建模

  1. 根布局:ConstraintLayout
  2. Child:滚动视图/嵌套滚动视图
  3. 嵌套滚动视图Child:约束布局
  4. 底部布局:LinearLayout(可以是RelativeLayout/ConstraintLayout)

通过从 NestedScrollview 中删除 android: fillViewPort = "true" 属性并在布局末尾添加一个空视图来解决此问题,如下所示:

<View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_20sdp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/clProgressSignUp"
    />

这使得屏幕可以滚动,并且在使用上面的布局并在 Activity 的清单中使用标志 android:windowSoftInputMode="adjustResize" 后满足了要求。