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

5
我在使用新的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 编辑器无法协作。

这个问题在未来的版本中会被修复吗?或者有什么方法可以避免这个问题?

感谢各位。


2
你是否在尝试使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)? http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TRANSLUCENT_STATUS - krystian71115
谢谢,那个可行。我知道这行代码,但在我的其他一些应用中,它不知何故不需要。请将此编写为答案,以便我可以将其置为已接受! :) - Lampione
2个回答

4

2
请将以下行添加到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开始支持。这是KitKat而不是Lollipop。 - krystian71115
请参考:http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TRANSLUCENT_STATUS - krystian71115
但是你不能通过上面的代码为KitKat设置透明状态栏。我只提供了Lollipop和后续版本设备的代码片段。还有一些外部库可用,比如systemtintedstatusbar,可以为KitKat设备提供半透明效果。 - Harsh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接