【发布时间】:2021-06-18 12:54:15
【问题描述】:
我有折叠工具栏和一些我想折叠的布局。为了防止视图进入状态栏,我使用系统插图设置折叠工具栏的边距。我将AppBarLayout 提取到单独的文件中并将其包含在CoordinatorLayout 中:
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_white"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorToolbar"
android:elevation="4dp"
android:focusable="true"
android:focusableInTouchMode="true"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin">
...
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.card.MaterialCardView
android:id="@+id/bonuses_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="14dp"
app:cardElevation="3dp"
app:layout_collapseMode="parallax">
...
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
MaterialCardView 的边距是从代码中设置的,因为布局不支持添加。我使用插图为折叠工具栏添加边距:
ViewCompat.setOnApplyWindowInsetsListener(action_bar) { _, insets ->
collapsing_toolbar.setMarginTop(insets.systemWindowInsetTop)
insets
}
这在折叠工具栏展开时效果很好,但在折叠状态下工具栏位于状态栏下方:
我已阅读this 问题,已实现AppBarStateChangeListener 并像这样使用它:
action_bar.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
override fun onStateChanged(appBarLayout: AppBarLayout, state: State) {
if (state == State.COLLAPSED) {
ViewCompat.setOnApplyWindowInsetsListener(collapsing_toolbar) { _, insets ->
toolbar.setMarginTop(insets.systemWindowInsetTop)
insets
}
}
if (state == State.EXPANDED) {
ViewCompat.setOnApplyWindowInsetsListener(collapsing_toolbar) { _, insets ->
toolbar.setMarginTop(0)
insets
}
}
}
})
这没有帮助,据我所知,状态栏的插图已经由操作栏处理,折叠工具栏没有什么可处理的。
我还尝试在折叠工具栏的同时向工具栏添加边距,它有效但导致了另一个问题:
毕竟我尝试删除插入并将android:fitsSystemWindows="true" 设置为操作栏,这解决了状态栏下工具栏的问题,但状态栏意外地获得了奇怪的颜色(紫色),这在应用程序颜色中没有表示。
希望有人知道在这种情况下如何正确处理插图。
【问题讨论】:
-
您是否为状态栏添加了颜色?您正在测试哪个操作系统?
-
This 可能会有所帮助。
-
@MuhammadZahabAhmadKhan 我没有为这个片段的状态栏提供任何特定的颜色,它具有应用程序定义的颜色。我正在 Android 8 上进行测试。
-
我的问题主要是关于插图,状态栏颜色应该由系统使用应用程序颜色设置。
-
你打电话给
setSupportActionBar()了吗?
标签: android kotlin android-toolbar android-collapsingtoolbarlayout insets