Jos*_*phW 3 android android-layout android-softkeyboard windowinsets android-bottomsheetdialog
如何使用最新的 WindowInset API来调整我的对话框和软键盘之间的空间?
我有一个带有一些 EditText 的 BottomSheetDialog。默认动画将在我的 EditText 正下方显示软键盘,它将覆盖我的保存按钮。在做了一些研究之后,我将这一行添加到了我的BottomSheetDialog
片段中
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Run Code Online (Sandbox Code Playgroud)
它起作用了(如下图所示)!
但显然SOFT_INPUT_ADJUST_RESIZE
已被弃用。
* @deprecated Call {@link Window#setDecorFitsSystemWindows(boolean)} with {@code false} and
* install an {@link OnApplyWindowInsetsListener} on your root content view that fits insets
* of type {@link Type#ime()}.
Run Code Online (Sandbox Code Playgroud)
而且我不知道如何使用 newOnApplyWindowInsetsListener
来达到相同的效果。这是我当前的BottomSheetDialog
片段:
public class BottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Adding this line works, but it's deprecated in API 30
// getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getDialog().getWindow().setDecorFitsSystemWindows(false);
view = inflater.inflate(R.layout.fragment_bottom_dialog_cash, container, false);
view.setOnApplyWindowInsetsListener((v, insets) -> {
Log.d("dialog", "onCreateView: ");
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
v.setPadding(0,0,0,imeInsets.bottom);
return insets;
});
return view;
}
Run Code Online (Sandbox Code Playgroud)
我在另一个片段中使用 onclicklistener 来显示此对话框。输入代码Another fragment
@Override
public void onItemClick(int position) {
BottomSheetDialog dialog = new BottomSheetDialog();
dialog.show(getParentFragmentManager(), "BottomSheetDialog");
}
Run Code Online (Sandbox Code Playgroud)
Zak*_*nov 10
兼容版本
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(rootView()) { _, insets ->
val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
rootView().setPadding(0, 0, 0, imeHeight)
insets
}
Run Code Online (Sandbox Code Playgroud)
或者使用Insetter库
WindowCompat.setDecorFitsSystemWindows(window, false)
rootView().applyInsetter {
type(ime = true) {
padding()
}
}
Run Code Online (Sandbox Code Playgroud)
经过更多研究,我发现如果我使用viewBinding
并替换view
为bind.getRoot()
,那么一切正常。我不确定为什么(也许我应该使用 inonViewCreated
而不是onCreateView
?)但代码应该对遇到相同问题的人有所帮助。
// NOTE: you have to set this in the activity instead of fragment.
getWindow().setDecorFitsSystemWindows(false);
// Only work with API30 or above!
bind.getRoot().setOnApplyWindowInsetsListener((v, insets) -> {
imeHeight = insets.getInsets(WindowInsets.Type.ime()).bottom;
bind.getRoot().setPadding(0, 0, 0, imeHeight);
return insets;
});
Run Code Online (Sandbox Code Playgroud)
需要注意的一件事是 setDecorFitsSystemWindows(false) 意味着应用程序(您)负责所有系统窗口,包括状态栏和导航栏。
我还发现下面链接的教程非常有用,请检查您是否想了解更多关于 windowInsets 和新动画回调的信息。
用于检查键盘 (IME) 可见性和大小的新 WindowInsets API
小智 5
唯一对我有用的事情是将 BottomSheetDialog 的样式设置为使用以下内容:
<style name="BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
</style>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3986 次 |
最近记录: |