android - 如何向我的 UI 添加填充,以避免在横向时被显示切口遮挡?

标签 android kotlin landscape-portrait device-orientation

display cut-out screenshot

我仍然希望底部导航栏保持相同的长度,但我想在左侧的容器中添加一些填充,以将卡片向右移动一点。我的 MainActivity.kt 现在有以下代码:

WindowCompat.setDecorFitsSystemWindows(window, false)
    ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = insets.top
        }
        binding.bottomNavigationView.updatePadding(bottom = insets.bottom)
        WindowInsetsCompat.CONSUMED
    }

我的 activity_main.xmlConstraintLayout 组成,其中包含两个元素:BottomNavigationViewFragmentContainerView

最佳答案

而不是填充布局 documentation为此提供默认的剪切模式选项。

LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT - With this default setting, content renders into the cutout area when displayed in portrait mode, but content is letterboxed when displayed in landscape mode

因此,您可以使用以下方式在主题/样式文件中指定它:

<item name="android:windowLayoutInDisplayCutoutMode">default</item>

更新

I have it set to shortEdges because I don't want the default behavior. The default behavior doesn't go edge-to-edge when in landscape orientation. The problem I'm having is that the cards get obscured, so I'm wondering how to fix that with either inset margins or padding.

由于添加填充的位置会根据设备方向发生变化,因此您可以使用 OrientationEventListener 跟踪方向方向的变化,并相应地添加填充。

剪切深度的典型值可以作为屏幕插入的最大值来发现(因为应用程序将以纵向、横向左/右启动);在每种情况下,切口位置都不同。

private var cutoutDepth = 0
WindowCompat.setDecorFitsSystemWindows(window, false)
    ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

        cutoutDepth = Collections.max(listOf(insets.bottom, insets.left, insets.right))

        // reset of code is omitted

请注意,cutoutDepth 变量应保存在像 ViewModel 这样的永久存储中;你需要处理这个问题。

这里将padding添加到 Root View 中;但您可以将其级联到 fragment 中的 RecyclerView

private val orientationListener by lazy {
    object : OrientationEventListener(applicationContext, SensorManager.SENSOR_DELAY_NORMAL) {
        override fun onOrientationChanged(orientation: Int) {

            if (orientation == ORIENTATION_UNKNOWN) return
           
            val rotation =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) display?.rotation
                else windowManager.defaultDisplay.rotation

            when (rotation) {
                Surface.ROTATION_0 -> {
                    // BOTTOM
                    binding.root.setPadding(0, 0, 0, 0) // reset the padding in portrait 
                    Log.d("LOG_TAG", "Orientation: BOTTOM")
                }
                Surface.ROTATION_90 -> {
                    // LEFT
                    binding.root.setPadding(cutoutDepth, 0, 0, 0)
                    Log.d("LOG_TAG", "Orientation: LEFT")
                }
                Surface.ROTATION_180 -> {
                    //  TOP
                    binding.root.setPadding(0, 0, 0, 0) // // reset the padding in upside down (if it's allowed)
                    Log.d("LOG_TAG", "Orientation: TOP")
                }

                Surface.ROTATION_270 -> {
                    //  RIGHT
                    binding.root.setPadding(0, 0, cutoutDepth, 0)
                    Log.d("LOG_TAG", "Orientation: RIGHT")
                }

            }

        }
    }
}

override fun onResume() {
    super.onResume()
    // start the orientation listener
    if (orientationListener.canDetectOrientation())
        orientationListener.enable()
}

override fun onPause() {
    super.onPause()
    // stop the orientation listener
    orientationListener.disable()
}

关于android - 如何向我的 UI 添加填充,以避免在横向时被显示切口遮挡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74439257/

相关文章:

android - 发送.txt文件,文档文件到android中的服务器

Android MediaRecorder 类 : Does the setOutputFile() method append data to the existing file?

android - setVisibility GONE 在 Listview Android 中不起作用

AndroidStudio 4.1 稳定版。调试器的问题

android - 如何在 android 中通过手势平滑滑动?

android - 使用Source.Server但用户未连接到互联网时,如何获取Firestore错误代码?

spring - @KafkaListener 启动问题(Spring)

android - 将自动完成 TextView 结果显示为键盘上方的建议

android - 当您从横向更改为纵向时,可以避免调用 onCreate 吗?

android - PagerSlidingTabStrip 横向模式填充修复