kotlin - fix top appbar color when scrolling after implementing edge to edge - Stack Overflow

After adding top.inset to top appbar the statusbar has some kind of lag when scrolling and it changes c

After adding top.inset to top appbar the statusbar has some kind of lag when scrolling and it changes colors. How can I solve this?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    enableEdgeToEdge(SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT))

    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.toolbar)) { v, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = insets.top
        }
        WindowInsetsCompat.CONSUMED
    }
}
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:transitionName="appbar"
    tools:ignore="UnusedAttribute">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?toolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        android:minHeight="?actionBarSize"
        app:titleTextAppearance="@style/TextAppearance.Frames.ToolbarTitle"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

</com.google.android.material.appbar.AppBarLayout>

After adding top.inset to top appbar the statusbar has some kind of lag when scrolling and it changes colors. How can I solve this?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    enableEdgeToEdge(SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT))

    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.toolbar)) { v, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = insets.top
        }
        WindowInsetsCompat.CONSUMED
    }
}
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:transitionName="appbar"
    tools:ignore="UnusedAttribute">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?toolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        android:minHeight="?actionBarSize"
        app:titleTextAppearance="@style/TextAppearance.Frames.ToolbarTitle"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

</com.google.android.material.appbar.AppBarLayout>
Share Improve this question edited Nov 21, 2024 at 11:05 tyg 16.2k4 gold badges36 silver badges48 bronze badges asked Nov 21, 2024 at 4:40 ThrowerThrower 11 bronze badge 5
  • Change id of setOnApplyWindowInsetsListener target view to AppBarLayout, or juet add "android:fitsSystemWindows="true"" to your AppBarLayout in xml, – Kolyneh Commented Nov 23, 2024 at 9:38
  • @Kolyneh its stop changing colors, but now appbar not hidding completely and I see some of its content under the status bar – Thrower Commented Nov 23, 2024 at 14:37
  • Then, add "android:fitsSystemWindows="true"" to your root layout. – Kolyneh Commented Nov 25, 2024 at 1:51
  • @Kolyneh it's not working, it just moves my bottom navbar up – Thrower Commented Nov 26, 2024 at 5:28
  • OK sorry, I didn't find that you're using updateLayoutParams, change it to updatePadding instead, or just remove this setOnApplyWindowInsetsListener call. – Kolyneh Commented Nov 26, 2024 at 5:56
Add a comment  | 

1 Answer 1

Reset to default 0

the problem was in android:layout_height="?actionBarSize" in androidx.appcompat.widget.Toolbar I changed it to android:layout_height="wrap_content" and the insets worked as they should

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313624a4420392.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信