onApplyWindowInsets(WindowInsets insets)没有被调用

onApplyWindowInsets(WindowInsets insets) not getting called

我目前正在使用扩展约束布局的自定义视图,但它不会在视图 onApplyWindowInsets(WindowInsets insets) 中触发这个被覆盖的方法,我不确定丢失了什么。

  class TestCustomView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {


    init {


    }
    //This method one not get called
    override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
    return super.onApplyWindowInsets(insets)
        val statusBarHeight = insets.systemWindowInsetTop
    }


    override fun fitSystemWindows(insets: Rect): Boolean {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
      // Intentionally do not modify the bottom inset. For some reason,
      // if the bottom inset is modified, window resizing stops working.
      insets.left = 0
      insets.top = 0
      insets.right = 0
    }
    return super.fitSystemWindows(insets)
  }


}

一旦插图被消耗,向下传播层次结构停止。看起来更高的东西正在消耗可用的东西。请参阅 WindowsInsetisConsumed()

Check if these insets have been fully consumed.

Insets are considered "consumed" if the applicable consume* methods have been called such that all insets have been set to zero. This affects propagation of insets through the view hierarchy; insets that have not been fully consumed will continue to propagate down to child views.

我想如果你想重置它的 windowInsets 回调状态,你应该改变 sysUiVisibility 参数让 fitsSystemWindows 正常工作。因为 fitsSystemWindows 是消耗 windowInsets 的键。您可以从android源代码中了解更多,当然,您需要时间。

我发现如果不将 android:windowTranslucentStatus 属性 设置为 true,则永远不会调用 onApplyWindowInsets。我在阅读 this unanswered question.

时发现了这一点

styles.xml中:

<style name="ExampleTheme" parent="Theme.AppCompat.NoActionBar">
  <item name="android:windowTranslucentStatus">true</item>
</style>

我很想知道为什么会这样,是否有一种方法可以在不需要更改此属性的情况下调用它。