Android4.4沉浸状态栏全解析

首先先说明沉浸状态栏的原理:

沉浸主要是通过设置value-v19的styles的android:windowTranslucentStatus

使状态栏透明,离状态栏最近的view则会填充状态栏

注意,这个是api19才能用的。


下面是步骤:


1.定义颜色和style

<span style="font-size:18px;"><span style="font-size:18px;"><resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorBackground">#ffffff</color>
</resources></span></span>

values/styles

<span style="font-size:18px;"><resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 要5.0以上的手机才支持android原生自定义状态栏颜色-->
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <!-- ToolBar颜色-->
        <item name="colorPrimary">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/colorBackground</item>
    </style>

</resources></span>


values-v19/styles(这里就是沉浸的实现操作)

<span style="font-size:18px;"><resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources></span>

当手机或模拟器是api19时就会额外调用此styles,这里主题便让状态栏透明,也可以采用java代码的方式

<span style="font-size:18px;">if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
                                //透明状态栏
                                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                                //透明导航栏
                                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                        }</span>


2.布局文件

activity_main

<span style="font-size:18px;"><android.support.v4.widget.DrawerLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <LinearLayout
        android:id="@+id/id_main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/id_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"/>


        <TextView
            android:id="@+id/id_tv_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Hello"/>
    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/id_nv_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header_just_username"
        app:menu="@menu/menu_drawer"
        />
</android.support.v4.widget.DrawerLayout></span>

当然用到了NavigationVIew变有headerLayout和menu,这里不是重点,就不贴代码了

这里有三点要注意:

(1)Toolbar的高度要设成wrap_content

(2)Toolbar要设置fitsSystemWindows="true"

(2)NavigationVIew的headerLayout的根布局要fitsSystemWindows="true"

  fitsSystemWindows="true"在这里设置的主要目的防止Toolbar和状态栏有挤压的状态


效果图



沉浸就这样实现了,但这还不是md的规范,md的规范是让状态栏和toolbar有颜色的突兀


md规范效果图



这里用到了一个开源库SystemBarTint

实现前提,也是要在values-v19设置android:windowTranslucentStatus

<span style="font-size:18px;">if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            activity.getWindow().setStatusBarColor(statusColor);
            return;
        }
        
        SystemBarTintManager mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setNavigationBarTintEnabled(true);
        mTintManager.setTintColor(Color.parseColor("#3F51B5"));</span>

当api>=21时

1.直接用api21的setStatusBarColor来设置状态栏颜色,

2.或者直接在app中的主题中设置<item name="colorPrimaryDark">@color/colorPrimary</item>

   当然这个是只有在api21以上才会生效的



以上是兼容4.4的状态栏沉浸,

接下来会说

状态栏沉浸如何和CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,ToolBar结合使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值