我的 DialogFragment 中有一个 EditText,当软键盘向上移动时,DialogFragment 会稍微向上移动,但被底部的键盘阻挡,我不介意,除非 EditText 位于底部并且它也被部分覆盖。 DialogFragment 的顶部向上移动时也会被切断。该对话框是从根布局显示的,因此不应期望任何东西会阻止它。
这是我的 onResume 和布局:
override fun onResume() {
super.onResume()
val width = resources.getDimension(R.dimen.popup_simple_width_small).toInt()
val height = ViewGroup.LayoutParams.WRAP_CONTENT
dialog?.window?.let { w ->
w.setLayout(width, height)
w.setBackgroundDrawable(
ResourcesCompat.getDrawable(
resources,
getThemeAttrDrawableId(requireContext(), R.attr.dialogBackground),
null
)
)
w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/dialogBackground">
<ImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/banner_image"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/close_button"
android:layout_width="@dimen/size_30"
android:layout_height="@dimen/size_30"
android:foreground="?android:selectableItemBackground"
app:srcCompat="@drawable/close_icon"
app:tint="@color/gray"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="@dimen/size_15"
android:layout_marginEnd="@dimen/size_15" />
<TextView
android:id="@+id/title"
style="@style/Heading2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_text"
android:textColor="?attr/primaryTextColor"
android:gravity="center"
android:layout_marginTop="@dimen/size_18"
android:layout_marginHorizontal="@dimen/size_20"
app:layout_constraintTop_toBottomOf="@id/banner"/>
<TextView
android:id="@+id/description"
style="@style/Normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/description_text"
android:textColor="?attr/secondaryTextColor"
android:gravity="center"
android:layout_marginTop="@dimen/size_12"
android:layout_marginHorizontal="@dimen/size_20"
app:layout_constraintTop_toBottomOf="@id/title"/>
<TextView
android:id="@+id/error_text"
style="@style/SmallBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="@color/error_red"
android:gravity="center"
android:layout_marginTop="@dimen/size_12"
app:layout_constraintTop_toBottomOf="@id/description"/>
<EditText
android:id="@+id/edit_text"
style="@style/Heading4"
android:layout_width="match_parent"
android:layout_height="@dimen/size_46"
android:hint="@string/hint_text"
android:textColor="@color/gray"
android:gravity="center"
android:maxLines="1"
android:background="@drawable/blue_background"
android:layout_marginTop="@dimen/size_8"
android:layout_marginHorizontal="@dimen/size_20"
app:layout_constraintTop_toBottomOf="@id/error_text"/>
<TextView
android:id="@+id/main_button"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="@dimen/size_40"
android:text="@string/button_text"
android:textColor="?attr/mainActionButtonTextColor"
android:gravity="center"
android:background="@drawable/purple_background"
android:layout_marginTop="@dimen/size_6"
android:layout_marginBottom="@dimen/size_20"
android:layout_marginHorizontal="@dimen/size_20"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试将窗口上的软输入模式设置为 SOFT*_*INPUT_ADJUST_PAN,但没有任何改变。该对话框之前是从子布局中显示的,我认为这可能是它从顶部被切断的原因,但将其移动到父视图后我仍然遇到同样的问题。
如果其他人遇到这个问题,我通过使用这个库KeyboardVisibilityEvent库检测键盘可见性来解决它,并使用这些来源的代码来动画和上下翻译对话框:翻译, 动画