android 将工具栏扩展为半透明状态栏,同时将其他布局对象保留在系统视图中

android extend toolbar into translucent status bar while keeping other layout objects in systemview

我正在尝试让选项卡式工具栏的背景渐变延伸到半透明状态栏上,如下图所示。

我曾尝试使用 LAYOUT_NO_LIMITS 和 TRANSLUCENT_NAVIGATION 标志来实现此目的,但是工具栏的一半位于状态栏下方,快餐栏位于导航栏下方。

这是当前的布局文件:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:background="@drawable/main_gradient">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/main_gradient"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@android:color/white"/>

    </android.support.design.widget.AppBarLayout>

    <View
        android:id="@+id/toolbar_shadow"
        android:layout_width="match_parent"
        android:layout_height="12dp"
        android:layout_below="@id/toolbar"
        android:background="@drawable/toolbar_dropshadow" />

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeContainer"
        android:layout_width="368dp"
        android:layout_height="495dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

如何将选项卡式工具栏渐变扩展到状态栏,同时仍保持正确的系统布局边界并保持常规彩色导航栏?

很久以前我遇到过类似的问题。解决它的简单方法是使用虚拟 View 将您的 Toolbar 和其他视图向下推。

虚拟视图:

查看状态栏的高度以将内容推送到下方。

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/status_bar_height"/>

现在布局变为:

<android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/main_content"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"
     tools:context="software.endeavor.projectg.TabbedActivity">


    <View
       android:layout_width="match_parent"
       android:layout_height="@dimen/status_bar_height"/>

    ...

</android.support.design.widget.CoordinatorLayout>

希望您有所了解。还要注意维度中的 status_bar_height 值,并注意状态栏高度在 Android M 或 N(不确定)中发生了变化。

参考这篇文章获取状态栏高度: Height of status bar in Android

放置 fitSystemWindows 的位置很重要。具有 属性 的视图将添加填充以确保系统组件(在本例中为状态栏)不会与您的内容重叠。

例如,如果您将它移至 Toolbar,则会为其添加填充,您将在状态栏下方填充 Toolbar

请记住,只有层次结构中具有 fitSystemWindows 的第一个视图才会添加填充。

这比使用虚拟视图和依赖获取状态栏的高度会带来其他几个问题要简单得多。

这方面的好文章available here