尝试边到边设计时 Android Material 工具栏与状态栏重叠

标签 android android-layout android-toolbar android-navigation-bar

我试图实现边缘到边缘设计以充分利用 Android Q 中的屏幕,但我遇到了一些问题。

我的代码如下:

Activity :

    <androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/Drawer_Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.main.MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/Layout_Coordinator_Main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutFullscreen="@{true}">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/Toolbar_Main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorOnSurface"
            android:clipChildren="false"
            android:outlineAmbientShadowColor="@color/colorShadowColor"
            android:outlineSpotShadowColor="@color/colorShadowColor"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            app:contentInsetStartWithNavigation="0dp">

...

绑定(bind)适配器:

@BindingAdapter("layoutFullscreen")
fun View.bindLayoutFullscreen(previousFullscreen: Boolean, fullscreen: Boolean) {
    if (previousFullscreen != fullscreen && fullscreen) {
        systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    }
}

v29\colors.xml(仅适用于 Android Q)

<resources>
    <!--Set the navigation bar to transparent on Q+ and allow the system to guard against-->
    <!--low contrast scenarios.-->
    <color name="nav_bar">@android:color/transparent</color>
</resources>

颜色.xml:

<!--Set the navigation bar to transparent on Q+ and allow the system to guard against-->
<!--low contrast scenarios.-->
<color name="nav_bar">@color/colorPrimaryDark</color>

主题.xml

    <!-- Status and navigation bar colors -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">@bool/theme_status_bar_light</item>
    <item name="android:navigationBarColor">@color/nav_bar</item>

导航效果很好,但是,我的工具栏与状态栏重叠,如下所示:

Status bar issue

我已经尝试过以下操作:

- <item name="android:windowDrawsSystemBarBackgrounds">false</item>
- android:fitsSystemWindows="true"

还是不行。

编辑

我也尝试过@Gabriele Mariotti 的回答

我在 onCreate() 中添加了以下代码:

val paddingTop = _binding.ToolbarMain.paddingTop
    ViewCompat.setOnApplyWindowInsetsListener(_binding.ToolbarMain){v, insets ->
        v.updatePadding(top = paddingTop + insets.systemWindowInsetTop)
        insets
    }

最终结果是这样的:

issue 2

我仍然丢失部分 Material 工具栏。

有人可以帮我吗?

最佳答案

您必须在 AppBarLayout 中处理 WindowInsets(如果您不使用的话,则在 Toolbar 中处理)。

类似于:

   val appbar : AppBarLayout = findViewById(R.id.appbar)
   val appbarTopPadding = appbar.paddingTop

   ViewCompat.setOnApplyWindowInsetsListener(appbar) { v, insets ->
         v.updatePadding(top = appbarTopPadding + insets.systemWindowInsets.top)
         insets
   }

与:

enter image description here

没有:

enter image description here

您可以找到更多info here .

关于尝试边到边设计时 Android Material 工具栏与状态栏重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62489177/

相关文章:

android - 如何使用 MVP 从 RecyclerView 插入/删除项目

javascript - IOS设备隐藏 "Play Store"图片,Android设备隐藏 "App Store"图片

android - 视频抓拍人脸检测-安卓

android - 不使用动画旋转布局

Android,如何知道我的 View 被软键盘adjustPan移动了​​多少

android - 如何单击工具栏后面的 View ?

java - 当在我的文本框中输入内容时,我的 ListView 过滤效果很好。但是,当我删除一些字母(退格键)时,列表不会更新

Android 设计支持 TabLayout 重叠内容

android - 工具栏中的自定义 View ,例如 Airbnb 探索屏幕

android - Fragment 中 onCreateView 和 onViewCreated 的区别