Android-在键盘上方显示BottomSheetDialogFragment [英] Android - Show BottomSheetDialogFragment above Keyboard

查看:1011
本文介绍了Android-在键盘上方显示BottomSheetDialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示带有一些EditText字段的BottomSheetDialogFragment供用户输入信息.我想直接在键盘上方显示它,但是它一直掩盖内容.

I'm trying to show a BottomSheetDialogFragment with a few EditText fields for the user to enter information. I want to show it directly above the keyboard, but it keeps covering up the contents.

这是我提起BottomSheetDialogFragment时发生的情况,您可以看到它选择了Card Number EditText,但覆盖了其他内容.

This is what happens when I bring up the BottomSheetDialogFragment, you can see it's selecting Card Number EditText, but covering the other content.

理想情况下,这就是我要查找的内容,您可以同时看到EditTexts和视图的填充.

Ideally, this is what I'm looking for, you can see both EditTexts, and the padding of the View.

我已经尝试过许多围绕windowSoftInputMode的解决方案,但是似乎没有任何效果.我通过父级Activity和实际的BottomSheetDialogFragment将其设置为adjustResize

I've tried a lot of solutions revolving around windowSoftInputMode, but nothing seems to work. I've set it to adjustResize for the parent Activity and the actual BottomSheetDialogFragment via

dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

我还尝试了修改布局,将其从FrameLayout更改为ScrollViewCoordinatorLayout,以查看是否对布局的位置产生了影响,但似乎没有任何效果

And I've also tried modifying my layout, changing it from a FrameLayout, to a ScrollView to a CoordinatorLayout to see if that had any effect on the position of the layout, but nothing seems to work.

如果有人知道如何做到这一点,将不胜感激,谢谢.

If anyone knows how to accomplish this, that would be greatly appreciated, thank you.

推荐答案

可能是一个较晚的答案,但可能会有所帮助.

Maybe a late answer but would probably help.

没有其他对我有用.但是此解决方案有效

Nothing else worked for me. But this solution is working

内部样式:

<style>  // main theme
    <item name="bottomSheetDialogTheme">@style/BottomSheetDialogTheme</item>
    ........ // rest of the code
</style>

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style>

此处android:windowIsFloating应为false& android:windowSoftInputMode必须为adjustResize

Here android:windowIsFloating should be false & android:windowSoftInputMode must be adjustResize

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_corner_dialog</item>
</style>

在NestedScrollView中包装布局

Wrap layout inside NestedScrollView

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

     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>

在某些设备上,此解决方案还不够.将此添加到代码中完全解决了我的问题.

On some devices this solution wasn't enough. Adding this to code solved my problem completely.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.setOnShowListener {
        val dialog = it as BottomSheetDialog
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet?.let { sheet ->
            dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
            sheet.parent.parent.requestLayout()
        }
    }
}

这篇关于Android-在键盘上方显示BottomSheetDialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆