ViewGroup inside CollapsingToolbarLayout show extra bottom padding when set fitsSystemWindows to be true

前端 未结 7 2516
有刺的猬
有刺的猬 2021-02-08 15:05

I am building an application and there is a profile fragment which shows a profile background image and some other elements. Like the screenshot below:

My quest

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-08 15:59

    Based on @Akshay answer this is my solution:

    In the xml file add android:fitsSystemWindows="true" to:

    1. CoordinatorLayout
    2. AppBarLayout
    3. ImageView (or other view you want to see behind the status bar. In my case it was a ConstraintLayout)

    In the Activity add @Akshay answer but edit it as follow (written in Kotlin):

     ViewCompat.setOnApplyWindowInsetsListener(appBarLayout) { _, insets ->
            // Instead of
            // toolbar.setPadding(0, insets.systemWindowInsetTop, 0, 0)
            (toolbar.layoutParams as ViewGroup.MarginLayoutParams).topMargin = insets.systemWindowInsetTop
            insets.consumeSystemWindowInsets()
        }
    

    Now the toolbar will have the proper height as well.

提交回复
热议问题