在尝试使用新的Android支持设计库时在我的应用中设置透明状态栏时,我遇到了一些问题。
具体来说,我正在使用CoordinatorLayout
+ AppBarLayout
+ CollapsingToolbarLayout
+ Toolbar
组合这种等级顺序。
我注意到状态栏首先是透明的,然后我可以看到从透明到全黑的转换。
我在值/样式中执行此操作:
<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编辑器不会合作)
这是否会在将来的版本中修复,或者有办法绕过这个?
谢谢大家。
答案 0 :(得分:4)
尝试设置半透明标志 - 它是在api级别19中添加的:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
编辑:它不能在低于19的api级别工作。(需要最低KitKat)
答案 1 :(得分: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