首页 > 解决方案 > 带有 softInputMode 的全屏 DialogFragment 调整调整大小

问题描述

所以我有一个 DialogFragment,我使用以下代码在 onResume 中扩展到整个屏幕:

    val params = dialog?.window?.attributes
    params?.width = ViewGroup.LayoutParams.MATCH_PARENT
    params?.height = ViewGroup.LayoutParams.MATCH_PARENT
    dialog?.window?.attributes = params as WindowManager.LayoutParams

我也使用以下样式:

<style name="AppTheme.FullscreenDialogFragment" parent="Theme.MaterialComponents.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowBackground">@color/background1</item>
</style>

现在我还希望它在状态栏下扩展,我通过以下方式执行此操作(我还将状态栏颜色设置为透明):

dialog?.window?.decorView?.systemUiVisibility =
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE.addFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

这一切都很好,但现在我想将 softInputMode 调整为 adjustResize。我就是无法让它工作。只有当我删除上面的布局标志时它才有效。它在正常片段中工作正常。有没有人知道我在这里缺少什么,或者 DialogFragment 是不可能的?

标签: androidandroid-dialogfragment

解决方案


所以我的问题不是DialogFragment,而是当你在系统栏下绘制布局时Android不尊重adjustResize的事实。为防止这种情况发生,您需要收听 Window insets 并将它们作为填充应用到您的布局中,以便当键盘打开时,底部 inset 会使您的整个内容变小,并且 ScrollView 可以滚动到底部。我将以下库用于插图:

https://github.com/chrisbanes/insette


推荐阅读