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

标签 android android-toolbar android-collapsingtoolbarlayout

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

具体来说,我使用的是 CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar 的组合.

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

我在值(value)观/风格中这样做:

<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 不会合作)

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

谢谢大家

最佳答案

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

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

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

编辑:它不能在低于 19 的 api 级别工作。(需要最低 KitKat)

关于android - 使用新的支撑设计库时状态栏不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249932/

相关文章:

android - 菜单图标不显示

java - 折叠工具栏布局: How to make a view disappear when collapsed

android - 设置 SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 使下一个 fragment 空白和折叠工具栏在 api <= 27 上不稳定

android - 使用 facebook sdk 在墙上发布照片

java - 如何在 fragment 中隐藏工具栏中的 OptionMenu?

android - 如何移植 android 以在满足 android 最低设备要求的 lg gt350 手机上运行

android - Eclipse Isse 中的 Material 抽屉

android - 工具栏不滚动

android - 如何让我的 Android ContentObserver for ContactsContract 检测到添加、更新或删除的联系人?

android - 在 C2DM 注册过程中将额外数据传递给 C2DMReceiver?