全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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

注意!沉浸式状态栏只支持安卓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) 

文本相关下载:点击免费下载源码及apk文件

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


# android  # 沉浸式状态栏  # 沉浸状态栏  # android4.4状态栏沉浸  # Android 实现沉浸式状态栏的方法  # Android 沉浸式状态栏与隐藏导航栏实例详解  # 解决Android 沉浸式状态栏和华为虚拟按键冲突问题  # Android沉浸式状态栏微技巧(带你真正理解沉浸式模式)  # Android 4.4以上"沉浸式"状态栏效果的实现方法  # Android App仿QQ制作Material Design风格沉浸式状态栏  # Android编程中沉浸式状态栏的三种实现方式详解  # Android 高仿QQ 沉浸式状态栏  # 另外两种Android沉浸式状态栏实现思路  # Android实现沉浸式状态栏功能  # 状态栏  # 背景色  # 第一种  # 要写  # 设置成  # 两种  # 不需  # 第二种  # 就可以  # 这是  # 在这里  # 好了  # 子类  # 第二个  # 先把  # 比较多  # 设置为  # 中就  # 来完成  # 大家多多 


相关文章: 如何通过建站之星自助学习解决操作问题?  制作电商网页,电商供应链怎么做?  建站主机服务器选型指南与性能优化方案解析  建站之星微信建站一键生成小程序+多端营销系统  制作网站的模板软件,网站怎么建设?  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  建站主机如何选?高性价比方案全解析  建站之星IIS配置教程:代码生成技巧与站点搭建指南  北京企业网站设计制作公司,北京铁路集团官方网站?  如何撰写建站申请书?关键要点有哪些?  电商网站制作公司有哪些,1688网是什么意思?  行程制作网站有哪些,第三方机票电子行程单怎么开?  如何确保西部建站助手FTP传输的安全性?  上海网站制作开发公司,上海买房比较好的网站有哪些?  高性能网站服务器部署指南:稳定运行与安全配置优化方案  宝塔面板如何快速创建新站点?  安云自助建站系统如何快速提升SEO排名?  如何在云主机上快速搭建网站?  建站与域名管理如何高效结合?  如何在腾讯云免费申请建站?  实例解析Array和String方法  建站之星如何助力企业快速打造五合一网站?  重庆网站制作公司哪家好,重庆中考招生办官方网站?  网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本  建站主机如何安装配置?新手必看操作指南  建站主机是否等同于虚拟主机?  视频网站制作教程,怎么样制作优酷网的小视频?  中山网站制作网页,中山新生登记系统登记流程?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  宿州网站制作公司兴策,安徽省低保查询网站?  C++中引用和指针有什么区别?(代码说明)  英语简历制作免费网站推荐,如何将简历翻译成英文?  ,网站推广常用方法?  公司网站制作价格怎么算,公司办个官网需要多少钱?  建站主机默认首页配置指南:核心功能与访问路径优化  如何正确选择百度移动适配建站域名?  黑客入侵网站服务器的常见手法有哪些?  Swift中循环语句中的转移语句 break 和 continue  网站网页制作专业公司,怎样制作自己的网页?  常州企业建站如何选择最佳模板?  制作证书网站有哪些,全国城建培训中心证书查询官网?  建站之星后台密码遗忘或太弱?如何重置与强化?  如何快速搭建高效可靠的建站解决方案?  如何选择域名并搭建高效网站?  教学论文网站制作软件有哪些,写论文用什么软件 ?  php条件判断怎么写_ifelse和switchcase的使用区别【对比】  制作农业网站的软件,比较好的农业网站推荐一下?  高防服务器租用如何选择配置与防御等级? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。