时间范围选择器与手机导航按钮重叠

4

我目前正在使用这个版本的Material库:

com.google.android.material:material:1.7.0-alpha02

然而,当我使用Material日期选择器显示日期范围时,选择器会重叠到手机上的导航按钮上,如下图所示:

来自S22 Ultra的屏幕截图


可能是由于fitsSystemWindows设置为true和透明导航栏。 - DrHowdyDoo
1个回答

5

这个重叠导航栏的原因是 MaterialDatePicker默认为全屏显示,除非有自定义样式,例如这个答案所示。但这也不是你想要的,因为它会像一个浮动对话框。

为了解决这个问题,你需要通过将 true 传递给 WindowCompat.setDecorFitsSystemWindows 来改变选择器窗口不适应其内容视图到窗口插图。如需详细信息,请参阅文档

但是,这也会影响状态栏的顶部插图,所以现在它看起来就像透明的;因此,你必须更改它的颜色以匹配选择器头背景的颜色。

以下是示例:

val picker = MaterialDatePicker.Builder.dateRangePicker().build()
picker.show(supportFragmentManager, "tag")

// Delay is used to give the dialog a chance to build as it would 
// return null unitl it's shown. MaterialDatePicker is a final class;
// so, this wouldn't be embedded in a custom extended class.
Handler(Looper.getMainLooper()).postDelayed({
    picker.dialog?.window?.apply {
        WindowCompat.setDecorFitsSystemWindows(this, true)
        statusBarColor = ResourcesCompat.getColor(resources, 
                                    R.color.purple_500,  /* default dialog head color */
                                    null)
    }
}, 100)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接