如何使状态栏不透明(Android 5)



我使用的是CoordinatorLayout和这(app:layout_scrollFlags="scroll|enterAlways")行代码,以便在滚动RecyclerView时隐藏工具栏。这也很好用,但是工具栏不会隐藏在状态栏后面,因为它是透明的。

我的布局是这样的

<?xml version="1.0" encoding="utf-8"?>
<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="com.yannisbecker.backup.MainActivity">
<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.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<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="center|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_backup_white_24dp"/>
</android.support.design.widget.CoordinatorLayout>

因为我已经搜索了互联网,只发现了很多关于"如何使状态栏透明"的事情,我现在在这里问,希望有人能帮助我。

因为我还没有足够的声誉,你可以在这里找到一个显示我的问题的图像

如果你想在滚动回收视图时隐藏状态栏下的工具栏,那么你必须改变状态栏的颜色,并从<CoordinatorLayout>标签中删除这个属性:android:fitsSystemWindows="true"

设置状态栏的颜色可以通过使用getWindow.setStatusBarColor()方法在MainActivity中完成:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark));
   }

AppTheme.NoActionBar样式的styles.xml文件中添加或编辑这个标签

<item name="android:statusBarColor">@android:color/color_of_your_choice</item>

最新更新