0

当我使用带有剪切的全屏时,我有下一个 屏幕。如何解决这个问题?

剪下:

window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

全屏:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (visible) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                )
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // прячем панель навигации
                or View.SYSTEM_UI_FLAG_FULLSCREEN // прячем строку состояния
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }
}

透明的

val color = ContextCompat.getColor(this, R.color.transparent_dark)
window.statusBarColor = color
window.navigationBarColor = color

默认情况下的 ActionBar(不是自定义工具栏):

<style name="AppTheme.Viewer" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="actionBarStyle">@style/MyTheme.ActionBar</item>
</style>

<style name="MyTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="elevation" tools:targetApi="lollipop">0dp</item>
    <item name="background">@color/transparent_dark</item>
</style>

谷歌没有帮助我。

4

1 回答 1

0

默认情况下,操作栏会处理 WindowInsets 并在系统元素应位于的位置添加填充(状态栏、导航栏和像我一样的剪切)。如果您将操作栏添加到布局中,您可以自己决定如何处理 WindowInsets。

themedAppBarLayout(R.style.MyTheme_Viewer_ActionBarOverlay) {
  id = Id.toolbar
  backgroundColorResource = R.color.transparent_dark

  doOnApplyWindowInstets { v, insets, _ ->
    v.updateLayoutParams<ViewGroup.MarginLayoutParams> {

      // Added indent only on top. 
      // ActionBar takes up the entire width of the screen without indentation.
      topMargin = insets.systemWindowInsetTop

      // If there is a cutout, we get its dimensions
      val cutoutRight = insets.displayCutout?.safeInsetRight ?: 0
      val cutoutLeft = insets.displayCutout?.safeInsetLeft ?: 0
      // Subtract the cutout size from WindowInsets, for fullscreen
      rightMargin = insets.systemWindowInsetRight - cutoutRight
      leftMargin = insets.systemWindowInsetLeft - cutoutLeft
    }
    insets
  }

  toolbar = toolbar {
    lparams(width = matchParent, height = wrapContent)
  }
}


fun View.doOnApplyWindowInstets(block: (View, WindowInsetsCompat, Rect) -> WindowInsetsCompat) {
  val initialPadding = recordInitialPaddingForView(this)

  ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets ->
    block(v, insets, initialPadding)
  }
  doFromSdk(20) {
    requestApplyInsetsWhenAttached()
  }
}


@RequiresApi(Build.VERSION_CODES.KITKAT_WATCH)
fun View.requestApplyInsetsWhenAttached() {
  if (isAttachedToWindow) {
    requestApplyInsets()
  } else {
    addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
      override fun onViewAttachedToWindow(v: View) {
        v.removeOnAttachStateChangeListener(this)
        v.requestApplyInsets()
      }

      override fun onViewDetachedFromWindow(v: View) = Unit
    })
  }
}


fun recordInitialPaddingForView(view: View) =
    Rect(view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom)
于 2020-02-24T10:52:37.383 回答