Android 显示键盘时防止DialogFragment调整/折叠大小
在显示键盘时,我一直无法使对话框片段不调整大小/折叠。我在Android 30这样的特定API中遇到过这个问题,但在Android 27这样的旧API中却没有。我在下面提供了屏幕截图,我可以确认他会影响我应用程序中特定API的所有对话框片段Android 显示键盘时防止DialogFragment调整/折叠大小,android,android-studio,android-fragments,dialog,dialogfragment,Android,Android Studio,Android Fragments,Dialog,Dialogfragment,在显示键盘时,我一直无法使对话框片段不调整大小/折叠。我在Android 30这样的特定API中遇到过这个问题,但在Android 27这样的旧API中却没有。我在下面提供了屏幕截图,我可以确认他会影响我应用程序中特定API的所有对话框片段 编辑:Tom Ladek指出这个问题只发生在SDK 30上。SDK的29及以下版本不受影响。关键可能在于对话框的创建。扩展DialogFragment时,只需创建该类型的新对象,对其调用show,即可。在该片段的onCreateView中,膨胀普通片段布
编辑:Tom Ladek指出这个问题只发生在SDK 30上。SDK的29及以下版本不受影响。关键可能在于对话框的创建。扩展
DialogFragment
时,只需创建该类型的新对象,对其调用show
,即可。在该片段的onCreateView
中,膨胀普通片段布局文件
因此,即使在API级别为30(或更高)的情况下,当打开软输入时,创建带有自定义片段的对话框的应用程序也不会调整其大小,可能如下所示:
main活动
:
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");
});
}
}
登录片段
:
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);
}
}
<?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>
活动\u 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>
您是否在清单中尝试了android:WindowsOfInputMode=“adjustNothing”?@AliEid是的,它有部分效果。它使我的晶圆厂按钮不再调整,但对话框片段仍然调整。值得注意的是,我的应用程序采用单活动架构。我可以确认,此问题不会影响30以下的API级别。在API级别为23、26、27、28和30的物理设备以及API级别为29和30的模拟设备上进行测试。通用解决方案(以任何方式设置
windowSoftInputMode
——清单、主题、布局、编程方式)似乎对API 30及以上版本的对话框没有影响。@TomLadek感谢您提供的信息!很高兴其他人也能复制这个问题。我也有同样的问题。我花了很多时间调查,但我也没能解决它。我还可以确认,这只发生在API 30上。不管是否使用了DialogFragment
或任何对话框构建器。只要涉及到对话框
类本身,或者更具体地说,如果通过对话框主题将windowIsFloating
属性设置为true
,就会出现问题。我的猜测是,这是Android 11中新的WindowInsets
API引入的行为变化(bug?)。感谢您的代码!遗憾的是,您的解决方案仍然“调整”或“调整”对话框片段。注意当键盘打开时对话框是如何向上移动的。当显示键盘时,所有低于30的SDK都不会导致对话框移动。主要的问题是,我的对话框中有一个滚动视图,因此当发生此调整/调整时,对话框会崩溃。我还测试了此代码,它仍然在API 30上为我调整对话框的大小。