当键盘出现时,如何防止DialogFragment调整大小/折叠

6

我一直在尝试解决对话框片段在键盘显示时不会调整大小/折叠的问题。我在特定API(例如Android 30)中遇到了这个问题,但在旧版本的API(例如Android 27)中并没有。 我在下面提供了屏幕截图,并确认它会影响我的应用程序中特定API的所有对话框片段。

Android API 30 对话框截图(调整大小问题)

Android API 27 对话框截图(期望的功能)

编辑:Tom Ladek指出此问题仅发生在SDK 30上。 SDK 29及以下版本不受影响。


你在清单文件中尝试过android:windowSoftInputMode="adjustNothing"吗? - private static
2
我可以确认这个问题不影响API 30以下的级别。在API级别23、26、27、28和30的物理设备以及API级别29和30的模拟设备上进行了测试。通用解决方案(以任何方式设置"windowSoftInputMode"-清单、主题、布局、编程)似乎对API 30及以上的对话框无效。 - Tom Ladek
1
@TomLadek 非常感谢您提供的信息!很高兴还有其他人也能够复制这个问题。 - Adam Sousa
嗨,我只是想补充一下,我也遇到了这个问题,我已经尝试了在Stack Overflow上找到的所有方法,但都没有解决。我也使用了MaterialAlertDialogBuilder。你有没有解决这个问题?@AdamSousa - Mackalester
1
我也遇到了完全相同的问题。我花了很多时间进行调查,但我也没有能够解决它。我还可以确认它只发生在API 30上。而且无论是否使用DialogFragment或任何对话框构建器都没有关系。只要涉及到Dialog类本身,甚至更具体地说,如果通过对话框主题将windowIsFloating属性设置为true,问题就会出现。我最好的猜测是,这是由于Android 11中新的WindowInsets API引入的行为变化(错误?)。 - Steven Meliopoulos
显示剩余4条评论
2个回答

0

我在我的应用程序中有同样的问题。 但是我发现我的对话框布局是ConstraintLayout并设置如下。

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"

我尝试了很多来自网络的解决方案。最终,我认为布局因键盘显示和父布局缩小而改变高度。 因此,我决定通过编程方式设置对话框的高度。 我在对话框 onStart 期间触发 setLayout 函数。我认为你可以将高度设置为设备屏幕高度。


0

关键可能在对话框的创建上。当您扩展DialogFragment时,您只需创建该类型的新对象,在其上调用show,就完成了。在该片段的onCreateView中,您可以填充普通的片段布局文件。

因此,一个使用自定义片段创建对话框的应用程序,即使在API级别30(及更高版本),也不会在打开软输入时调整大小,可能看起来像这样:

Screen cast taken on API level 30

MainActivity:

public class MainActivity extends androidx.appcompat.app.AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.show_dialog).setOnClickListener(v -> {
            new LoginFragment().show(getSupportFragmentManager(), "login");
        });
    }
}

LoginFragment

public class LoginFragment extends androidx.fragment.app.DialogFragment.DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dialogcontent, container, false);
    }
}

activity_main:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/show_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show dialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment_dialogcontent:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="300dp"
    android:padding="16dp">

    <TextView
        android:id="@+id/login_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toTopOf="@+id/login_description"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/login_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Please login"
        app:layout_constraintBottom_toTopOf="@+id/login_username"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_title" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_username"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:hint="Username"
        app:errorEnabled="true"
        app:layout_constraintBottom_toTopOf="@+id/login_password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_description">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_edittext_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="username"
            android:inputType="text|number" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Password"
        app:endIconMode="password_toggle"
        app:errorEnabled="true"
        app:layout_constraintBottom_toTopOf="@+id/login_button_ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_username">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_edittext_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="password"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/login_button_ok"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Login"
        app:layout_constraintBottom_toTopOf="@+id/login_button_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_password" />

    <Button
        android:id="@+id/login_button_cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Cancel"
        app:layout_constraintBottom_toTopOf="@+id/login_register_link"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_button_ok" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/login_register_link"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Register"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_button_cancel" />

</androidx.constraintlayout.widget.ConstraintLayout>

感谢您的代码!不幸的是,您的解决方案仍然会“调整”或“调整大小”对话片段。请注意键盘打开时对话框向上移动的情况。所有低于30的SDK在显示键盘时都不会导致对话框移动。主要问题是我的对话框内有一个滚动视图,因此当发生此调整大小/调整大小时,对话框会折叠。 - Adam Sousa
1
我也测试了这段代码,在API 30上仍然可以调整对话框的大小。 - Steven Meliopoulos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接