当前位置: 移动技术网 > 移动技术>移动开发>Android > Android之沉浸式状态栏的实现方法、状态栏透明

Android之沉浸式状态栏的实现方法、状态栏透明

2019年07月24日  | 移动技术网移动技术  | 我要评论
现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法 注意!沉浸式状态栏只支持安卓4.4及以上的版本 状态栏:4.4上是渐变色,5.0上是

现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法

注意!沉浸式状态栏只支持安卓4.4及以上的版本

状态栏:4.4上是渐变色,5.0上是完全透明,本文模拟器为4.4演示

效果图:

这里写图片描述

注意!两种方法的区别:

第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:

这里写图片描述 

第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:

这里写图片描述

第一种使用方法:

第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 translucent system bar 风格的theme,如下图:

这里写图片描述

values/style.xml:

<style name="translucenttheme" parent="apptheme">
  <!--在android 4.4之前的版本上运行,直接跟随系统主题-->
</style>

values-v19/style.xml:

<style name="translucenttheme" parent="theme.appcompat.light.darkactionbar">
  <item name="android:windowtranslucentstatus">true</item>
  <item name="android:windowtranslucentnavigation">true</item>
</style>

values-v21/style.xml:

<style name="translucenttheme" parent="theme.appcompat.light.darkactionbar">
  <item name="android:windowtranslucentstatus">false</item>
  <item name="android:windowtranslucentnavigation">true</item>
  <!--android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
  <item name="android:statusbarcolor">@android:color/transparent</item>
</style>

第二、在清单文件中配置需要沉浸式状态栏的activity加入theme

<activity android:name=".imageactivity" android:theme="@style/translucenttheme" />
<activity android:name=".coloractivity" android:theme="@style/translucenttheme" />

第三、在activity的布局文件中的跟布局加入“android:fitssystemwindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:

1.当背景为图片时,布局可以这么写:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/imgs_bj"
  android:fitssystemwindows="true">

</relativelayout>

效果:

2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/colorprimary" //根布局背景设置成“标题布局”想要的颜色
  android:fitssystemwindows="true"
  android:orientation="vertical">

  <!--标题布局-->
  <relativelayout
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:background="@color/color_31c27c">

    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerinparent="true"
      android:text="这是标题"
      android:textcolor="@android:color/white"
      android:textsize="20sp" />

  </relativelayout>

  <!--内容布局-->
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" //内容区域背景设置成白色
    android:gravity="center"
    android:orientation="vertical">

    <button
      android:layout_margintop="120dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:text="显示信息"
      android:onclick="showmsg"
      />
  </linearlayout>

</linearlayout>

效果图:

这里写图片描述

好了,以上就是沉浸式状态栏实现的全过程,但是还有一点值得注意的就是,如果我们activity比较多,每一个页面都添加android:fitssystemwindows="true" 比较麻烦,我们需要改动一下:

写一个基类basecoloractivity.class,代码如下:

public abstract class basecoloractivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    //这一行注意!看本文最后的说明!!!!
    supportrequestwindowfeature(window.feature_no_title);

    setcontentview(getlayoutresid());//把设置布局文件的操作交给继承的子类

    viewgroup contentframelayout = (viewgroup) findviewbyid(window.id_android_content);
    view parentview = contentframelayout.getchildat(0);
    if (parentview != null && build.version.sdk_int >= 14) {
      parentview.setfitssystemwindows(true);
    }
  }

  /**
   * 返回当前activity布局文件的id
   *
   * @return
   */
  abstract protected int getlayoutresid();

}

然后需要沉浸状态栏的activity继承该基类:

public class coloractivity extends basecoloractivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    //这里不需要写setcontentview()!
  }

  @override
  protected int getlayoutresid() {
    //oncreate的方法中不需要写setcontentview(),直接把当前activity的布局文件在这里返回就行了!
    return r.layout.activity_color;
  }
}

然后需要沉浸状态栏的activity的布局文件中就可以把android:fitssystemwindows="true"这行代码给省略了!

第二种使用方法(未完):

这里写图片描述 

写个工具类statusbarcompat.class:

public class statusbarcompat {

  private static final int invalid_val = -1;
  private static final int color_default = color.parsecolor("#20000000");

  @targetapi(build.version_codes.lollipop)
  public static void compat(activity activity, int statuscolor)
  {

    //当前手机版本为5.0及以上 
    if (build.version.sdk_int >= build.version_codes.lollipop)
    {
      if (statuscolor != invalid_val)
      {
        activity.getwindow().setstatusbarcolor(statuscolor);
      }
      return;
    }

    //当前手机版本为4.4
    if (build.version.sdk_int >= build.version_codes.kitkat && build.version.sdk_int < build.version_codes.lollipop)
    {
      int color = color_default;
      viewgroup contentview = (viewgroup) activity.findviewbyid(android.r.id.content);
      if (statuscolor != invalid_val)
      {
        color = statuscolor;
      }
      view statusbarview = new view(activity);
      viewgroup.layoutparams lp = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent,
          getstatusbarheight(activity));
      statusbarview.setbackgroundcolor(color);
      contentview.addview(statusbarview, lp);
    }

  }

  public static void compat(activity activity)
  {
    compat(activity, invalid_val);
  }


  public static int getstatusbarheight(context context)
  {
    int result = 0;
    int resourceid = context.getresources().getidentifier("status_bar_height", "dimen", "android");
    if (resourceid > 0)
    {
      result = context.getresources().getdimensionpixelsize(resourceid);
    }
    return result;
  }

}

使用方法:

在当前activity的oncreate中,调用方法statusbarcompat.compat就可以了:

//第二个参数是想要设置的颜色
statusbarcompat.compat(this, color.red);

如果嫌每个activity都要写有点麻烦,那就写个基类来完成这一步:

public class baseactivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    supportrequestwindowfeature(window.feature_no_title);
    super.oncreate(savedinstancestate);

    statusbarcompat.compat(this, color.red);
  }
}

然后每个activity的页面继承该baseactivity就可以了!

关于上面代码中提示注意的那个地方的说明:

隐藏系统title注意的两点:

1、继承appcompatactivity时使用:

supportrequestwindowfeature(window.featurenotitle)

2、继承activity时使用:

requestwindowfeature(window.featurenotitle) 

文本相关下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网