当 Toolbar 和 windowTranslucentStatus 设置为 true 时,软键盘在 fragment 中向上滚动视图

Scroll view when soft keyboard is up in fragment with Toolbar and windowTranslucentStatus set to true

片段在键盘弹起时不滚动,同时类似的设置适用于活动。我在托管片段的 activity 中添加了 android:windowSoftInputMode="adjustResize|stateAlwaysHidden,并将 android:fitsSystemWindows="true" 添加到片段的根布局。我正在使用 Butterknife。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/bg_create_account">

<View
    android:id="@+id/vail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black_two" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:paddingTop="@dimen/status_bar_height">

        <TextView
            style="@style/ProximaNova18PtWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:lineSpacingExtra="4sp"
            android:text="@string/create_account"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="16sp" />


    </androidx.appcompat.widget.Toolbar>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/inner_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/username_layout"
                style="@style/LoginTextInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginEnd="@dimen/content_margin"
                android:hint="@string/username"
                app:hintAnimationEnabled="true"
                app:hintTextAppearance="@style/ProximaNova14Pt"
                app:hintTextColor="@color/white">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/username_edit"
                    style="@style/LoginEdittextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:importantForAutofill="noExcludeDescendants"
                    android:inputType="textEmailAddress"
                    android:textColorHint="@color/white"
                    tools:text="dewe" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/email_layout"
                style="@style/LoginTextInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="@dimen/content_margin"
                android:hint="@string/email"
                app:hintAnimationEnabled="true"
                app:hintTextAppearance="@style/ProximaNova14Pt"
                app:hintTextColor="@color/white">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/email_edit"
                    style="@style/LoginEdittextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:importantForAutofill="noExcludeDescendants"
                    android:inputType="textEmailAddress"
                    android:textColorHint="@color/white"
                    tools:text="dewe" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/password_layout"
                style="@style/LoginTextInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="@dimen/content_margin"
                android:hint="@string/password"
                app:hintAnimationEnabled="true"
                app:hintTextAppearance="@style/ProximaNova14Pt"
                app:hintTextColor="@color/white">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/password_edit"
                    style="@style/LoginEdittextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:importantForAutofill="noExcludeDescendants"
                    android:inputType="textPassword"
                    android:textColorHint="@color/white"
                    tools:text="dewe" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/confirm_password_layout"
                style="@style/LoginTextInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="@dimen/content_margin"
                android:hint="@string/confirm_password"
                app:hintAnimationEnabled="true"
                app:hintTextAppearance="@style/ProximaNova14Pt"
                app:hintTextColor="@color/white">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/confirm_password_edit"
                    style="@style/LoginEdittextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:importantForAutofill="noExcludeDescendants"
                    android:inputType="textPassword"
                    android:textColorHint="@color/white"
                    tools:text="dewe" />

            </com.google.android.material.textfield.TextInputLayout>

            <androidx.appcompat.widget.AppCompatCheckBox
                android:id="@+id/checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="@dimen/content_margin"
                android:checked="true"
                android:hint="@string/i_agree_to_all_the_terms_and_conditions"
                android:padding="0dp"
                android:textAppearance="@style/ProximaNova14Pt"
                android:textColorHint="@color/white"
                app:buttonTint="@color/white" />


            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/create_account_button"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginStart="@dimen/content_margin"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="@dimen/content_margin"
                android:background="@drawable/bg_login_button"
                android:fontFamily="@font/proxima_nova_regular"
                android:lineSpacingExtra="4sp"
                android:text="@string/create_account_button"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <FrameLayout
                android:id="@+id/terms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/text_view_button_selector"
                android:layout_marginBottom="@dimen/default_margin"
                android:padding="@dimen/default_margin_half">

                <TextView
                    style="@style/ProximaNova14Pt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/view_terms_and_conditions" />

            </FrameLayout>

            <View
                android:id="@+id/stub"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:visibility="gone" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

</FrameLayout>

这是清单中的一个例外:

<activity
        android:name=".view.EntranceActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.Launcher"
        android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />

以及托管片段的 Activity:

@Layout(R.layout.activity_entrance)

public class 入口Activity 延伸基地Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    router.newRootScreen(new Screens.LoginScreen());
}

@Override
protected void callInjections() {
    injection().inject(this);
}

在我从托管该片段的 activity 中删除这一行后,它起作用了:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

这是在设置全屏模式,因此 adjustResize 无法工作