使用新的支撑设计库时状态栏不透明

Status bar not transparent when using new support design library

我在尝试使用新的 android 支持设计库时在我的应用程序中设置透明状态栏时遇到了一些问题。

具体来说,我使用的组合 CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar 具有此层次结构顺序。

我注意到状态栏先变透明然后我可以看到从透明到全黑的转换。

我在 values/styles 中这样做:

<item name="android:windowTranslucentStatus">true</item>

这在我的布局中:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:elevation="4dp"
    android:background="?android:colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:expandedTitleMarginStart="64dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            app:cardCornerRadius="4dp"
            app:cardElevation="6dp">

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Latest Stories"/>

                <TextView
                    android:id="@+id/latest_story_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title Example"/>

                <TextView
                    android:id="@+id/latest_story_preview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Content Example, this should be around 250 chars"/>

            </LinearLayout>

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

    </LinearLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_add_story"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:fabSize="normal"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:layout_margin="16dp"/>

<View
    android:id="@+id/ripple_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    android:elevation="10dp"
    android:stateListAnimator="@null"
    android:clickable="false"/>

(抱歉,格式很糟糕,SO editor 好像不会合作)

这个问题会在未来的版本中得到修复还是有办法绕过这个问题?

谢谢大家

将此行添加到 values/styles。xml

 <style name="MyTheme" parent="Base.MyTheme"></style>


     <style name="Base.MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
   <item name="colorPrimary">@color/primary_500</item>

   <item name="colorPrimaryDark">@color/primary_700</item>

   <item name="colorAccent">@color/accent_500</item>
   <item name="windowActionBar">false</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/background_material_light</item>

将以下代码添加到 values-v21/styles.xml

<style name="MyTheme" parent="Base.MyTheme">
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
 </style>

注意:此方法只能在Lollipop及以上API上设置半透明状态

将应用程序级别的主题设置为 MyTheme

尝试设置半透明标志 - 它是在 api 级别 19 中添加的:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TRANSLUCENT_STATUS

编辑:它在 api 级别低于 19 时不起作用。(需要最低 KitKat)