美文网首页
Android 杂记 - 沉浸式通知栏

Android 杂记 - 沉浸式通知栏

作者: 琼珶和予 | 来源:发表于2018-06-23 10:56 被阅读0次

      今天记录一下沉浸式通知栏的实现。在Android中实现沉浸式通知栏有很多种方式,今天只讲解一种。本方式在Android 5.0 ~ Android 8.0的真机上均测试过,都没有问题,别问我为什么不测5.0以下的,因为找不到!!!
      先来贴一张在小米note3上的截图,系统是Android 7.1.1:



      整个布局中,上面的TitleBar其实就是一个TextView,然后TextView背景颜色设置成为了闷骚的紫色,我们发现TextView的骚紫色成功扩展到我们状态栏上。我们来看看是怎么实现的吧

    1.新建styles文件

      首先我们得建立三套不同的styles,分别是styles,styles-v19,styles-v21。分别兼容19以上的设备和21以上的设备。



      其中,在v19的文件中,这样写:

        <style name="ImmerseTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowTranslucentStatus">true</item>
            <item name="android:windowTranslucentNavigation">true</item>
        </style>
    

      在v21中的文件中这样写:

        <style name="ImmerseTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowTranslucentStatus">false</item>
            <item name="android:windowTranslucentNavigation">true</item>
            <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
            <item name="android:statusBarColor">@android:color/transparent</item>
        </style>
    

      在正常的版本中任何东西都不写:

        <style name="ImmerseTheme" parent="AppTheme">
        </style>
    

    2.Activity的布局

      在布局文件中,注意要使用fitsSystemWindows属性,比如说下面是

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/demo"
    
        >
        <TextView
            android:gravity="center"
            android:fitsSystemWindows="true"
            android:background="#FF00FF"
            android:text="123"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.constraint.ConstraintLayout>
    

      这个布局文件展示出来的效果就是上面的demo图效果(注意,在AndroidMainifest文件theme改为我们自定义的theme)。
      一旦我们将TextView的fitsSystemWindows属性移除,那么就出现这样的效果了。



      现在我们来验证一下,当TextView设置了fitsSystemWindows属性之后是,高是被动态的增加了,还是真正把状态栏空出来。在Activity中我们写这样的代码就能测出我们空间的高度。

            final TextView textView = findViewById(R.id.textView);
            textView.post(new Runnable() {
                @Override
                public void run() {
                    Log.i("pby123", textView.getMeasuredHeight()+"");
                }
            });
    

      我们现在来看看设置了fitsSystemWindows属性和不设置的log值。注意,我除了改变TextView的fitsSystemWindows属性,其他的属性都没有改变。
      这是不设置fitsSystemWindows属性的:



      这是设置fitsSystemWindows属性的:



      我们发现TextView的高度是动态增加了。如果还不信的话,我们来测量一下状态栏的高度,通过一下代码可以获取状态栏的高度:
                    int resourceId = getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
                    Log.i("pby123", "status bar height = " + getResources().getDimensionPixelSize(resourceId));
    

      我们来看看日志:



      状态栏的高度正好等于TextView增加的高度。
      这就是实现沉浸式通知栏的一种方式。

    相关文章

      网友评论

          本文标题:Android 杂记 - 沉浸式通知栏

          本文链接:https://www.haomeiwen.com/subject/xlulyftx.html