首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让Android BottomSheetDialogFragment出现在系统/手势栏后面?

如何让Android BottomSheetDialogFragment出现在系统/手势栏后面?
EN

Stack Overflow用户
提问于 2021-08-26 11:48:34
回答 1查看 176关注 0票数 0

有很多解决方案可以在安卓导航栏后面实现绘图,其中一些来自Google itself,然而,没有一个可以开箱即用地在导航/手势栏后面显示底部页面。

要做到这一点,需要什么?

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 11:48:34

我们从一个片段开始:

代码语言:javascript
运行
复制
internal open class MyBottomSheetDialogFragment : BottomSheetDialogFragment() {
    private var myDialog: MyBottomSheetDialog? = null

    val dialog: MyBottomSheetDialog
        get() = myDialog ?: error("Dialog not yet created")

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
        MyBottomSheetDialog(requireContext()).also { dialog ->
            this.myDialog = dialog
        }
}

这是对话框的实现:

代码语言:javascript
运行
复制
internal class MyBottomSheetDialog(context: Context) :
    BottomSheetDialog(context, R.style.MyBottomSheetDialogTheme) {

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()

        val window = window
            ?: error("window not attached?!")
        val sheetView = findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            ?: error("bottom sheet design not found")

        // ensure the navigation buttons / bar is rendered dark for a light sheet background
        // and no, setting android:windowLightNavigationBar=true on the theme does _not_ work
        WindowInsetsControllerCompat(window, sheetView)
            .isAppearanceLightNavigationBars = true

        // as soon as we have the actual insets, move the sheet down below the 
        // navigation area and give it back some padding so its contents are rendered
        // above
        ViewCompat.setOnApplyWindowInsetsListener(sheetView) { _, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            sheetView.updatePadding(
                bottom = insets.bottom
            )
            sheetView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                bottomMargin = -insets.bottom
            }
            WindowInsetsCompat.CONSUMED
        }
    }
}

最后,这是主题:

代码语言:javascript
运行
复制
    <style name="MyBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/MyBottomSheetStyle</item>
        <!-- ATTENTION: BottomSheetDialog needs _both_, this attribute set to true _and_ 
             a (semi)transparent navigation bar color, to enable edge-to-edge mode. -->
        <item name="enableEdgeToEdge">true</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="shapeAppearanceLargeComponent">@style/MyBottomSheetShapeAppearance</item>
    </style>

    <style name="MyBottomSheetStyle" parent="Widget.MaterialComponents.BottomSheet.Modal">
        <item name="android:windowIsFloating">false</item>
        <item name="backgroundTint">@color/the_sheets_background_color</item>
    </style>

    <style name="MyBottomSheetShapeAppearance" parent="ShapeAppearance.MaterialComponents.LargeComponent">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">8dp</item>
    </style>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68937977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档