onApplyWindowInsets(WindowInsets insets) 没有被调用

Chi*_*nda 4 android-custom-view kotlin android-constraintlayout

我目前正在使用扩展约束布局的自定义视图,但它不会在视图 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)
  }


}
Run Code Online (Sandbox Code Playgroud)

hum*_*ker 6

我发现如果没有将android:windowTranslucentStatus属性设置为true,onApplyWindowInsets则永远不会被调用。我在阅读这个悬而未决的问题时发现了这一点。

styles.xml

<style name="ExampleTheme" parent="Theme.AppCompat.NoActionBar">
  <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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