沉浸式状态栏

Google从android kitkat(Android 4.4)開始,给我们开发人员提供了一套能透明的系统ui样式给状态栏和导航栏,这种话就不用向曾经那样每天面对着黑乎乎的上下两条黑栏了。还能够调成跟Activity一样的样式,形成一个完整的主题,和IOS7.0以上系统一样了。

首先看下效果

首先看下第一种方式

  • 系统的方式沉浸式状态栏实现
  • 步奏一
//当系统版本号为4.4或者4.4以上时能够使用沉浸式状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
  • 步奏二
       布局加入:
android:fitsSystemWindows="true"
android:clipToPadding="true"

我们看下activity和布局文件

FirstActivity.java:

 /**
* 沉浸式状态栏
*/
private void initState() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}

activity_first.xml:

<?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"> <TextView
android:fitsSystemWindows="true"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="140dp"
android:textSize="24dp"
android:background="@color/mask_tags_1"
android:text="你好。沉浸式状态栏"/> </LinearLayout>

接着看下另外一种方式

实现思路,加入隐藏布局,然后我们动态的计算状态栏的高度,然后把这个高度设置成这个隐藏的布局的高度。便能够实现

在这里我们通过反射来获取状态栏的高度

/**
* 通过反射的方式获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

来看下SecondActivity和布局文件吧

SecondActivity.java

package com.example.translucentbarstest;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout; import java.lang.reflect.Field; /**
* Created by 若兰 on 2016/1/22.
* 一个懂得了编程乐趣的小白,希望自己
* 能够在这个道路上走的非常远,也希望自己学习到的
* 知识能够帮助很多其它的人,分享就是学习的一种乐趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/ public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
initState();
} /**
* 动态的设置状态栏 实现沉浸式状态栏
*
*/
private void initState() { //当系统版本号为4.4或者4.4以上时能够使用沉浸式状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//
LinearLayout linear_bar = (LinearLayout) findViewById(R.id.ll_bar);
linear_bar.setVisibility(View.VISIBLE);
//获取到状态栏的高度
int statusHeight = getStatusBarHeight();
//动态的设置隐藏布局的高度
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linear_bar.getLayoutParams();
params.height = statusHeight;
linear_bar.setLayoutParams(params);
}
} /**
* 通过反射的方式获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}

activity_second.xml:

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.translucentbarstest.TwoActivity"> <!--这个是隐藏的布局,然后通过动态的设置高度达到效果-->
<LinearLayout
android:id="@+id/ll_bar"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:orientation="vertical"
android:background="#e7abff"
android:visibility="gone">
</LinearLayout>
<TextView
android:fitsSystemWindows="true"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="@color/mask_tags_3"
android:text="你好,沉浸式状态栏"/> </LinearLayout>

接下来看下第三种

这个是用的github上的第三方库

步奏一
              android:fitsSystemWindows="true"
android:clipToPadding="true
步奏二
               SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint 激活导航栏
tintManager.setNavigationBarTintEnabled(true);
//设置系统栏设置颜色
//tintManager.setTintColor(R.color.red);
//给状态栏设置颜色
tintManager.setStatusBarTintResource(R.color.mask_tags_1);
//Apply the specified drawable or color resource to the system navigation bar.
//给导航栏设置资源
tintManager.setNavigationBarTintResource(R.color.mask_tags_1);

来看下代码吧

ThreeActivity.java

package com.example.translucentbarstest;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager; import com.readystatesoftware.systembartint.SystemBarTintManager; /**
* Created by 若兰 on 2016/1/22.
* 一个懂得了编程乐趣的小白,希望自己
* 能够在这个道路上走的非常远。也希望自己学习到的
* 知识能够帮助很多其它的人,分享就是学习的一种乐趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/ public class ThreeActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint 激活导航栏
tintManager.setNavigationBarTintEnabled(true);
//设置系统栏设置颜色
//tintManager.setTintColor(R.color.red);
//给状态栏设置颜色
tintManager.setStatusBarTintResource(R.color.mask_tags_1);
//Apply the specified drawable or color resource to the system navigation bar.
//给导航栏设置资源
tintManager.setNavigationBarTintResource(R.color.mask_tags_1);
}
}
}

activity_three.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:orientation="vertical"
tools:context="com.example.translucentbarstest.ThirdActivity"> <TextView
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="@color/mask_tags_5"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:text="你好。沉浸式状态栏"
android:textSize="24dp"/>
</LinearLayout>

好了。原来自己以为沉浸式状态栏听着好厉害(有可能自己原先不知道),可是真正自己去做了,去了解了。也没有那么难、那么神奇了,我想这也是自己成长了一些。

继续努力。这个是上传的github上的demohttps://github.com/wuyinlei/-,如有疑问,欢迎交流

Android 沉浸式状态栏的三种实现方式的更多相关文章

  1. android 沉浸式状态栏的实现

    本文介绍一种简单的实现沉浸式状态栏的方法,要高于或等于api19才可以. 实现android沉浸式状态栏很简单,添加代码两步就可以搞定. 一.在activity中添加 getWindow().addF ...

  2. [置顶] Xamarin android沉浸式状态栏

    虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...

  3. Android沉浸式状态栏(透明状态栏)最佳实现

    Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...

  4. Android 沉浸式状态栏 实现方式二 ( 更简单 )

    以前写过一个沉浸式状态栏 的实现方式 Android 沉浸式状态栏 实现方式一 现在有个更为简单的实现方式 . 相关链接 http://www.apkbus.com/forum.php?mod=vie ...

  5. Android 沉浸式状态栏完美解决方案

    现在搜索Android 沉浸式状态栏,真的是一堆一堆,写的特别多,但是真正用的舒服的真没有,在这里自己整理一下开发记录 注意,在使用这个步骤过程之前,请把之前设置的代码注释一下 把布局带有androi ...

  6. 【Android实战】Android沉浸式状态栏实现(下)

    之前的Android沉浸式状态栏实现并没有考虑软键盘的影响,接下来的内容将会针对这个问题给出解决方式,先看一下效果图 这个是一个留言板的效果图: 即弹出软键盘的时候并不会导致整个布局上移. 详细怎样实 ...

  7. Android沉浸式状态栏实现

    Step1:状态栏与导航栏半透明化 方法一:继承主题特定主题 在Android API 19以上可以使用****.TranslucentDecor***有关的主题,自带相应半透明效果 例如: < ...

  8. Android 沉浸式状态栏攻略 让你的状态栏变色吧

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48649563: 本文出自:[张鸿洋的博客] 一.概述 近期注意到QQ新版使用了 ...

  9. Android沉浸式状态栏兼容4.4手机的实现

    一.概述 最近注意到QQ新版使用了沉浸式状态栏,ok.先声明一下:本篇博客效果下图: 关于这个状态栏变色究竟叫「Immersive Mode」/「Translucent Bars」有兴趣能够去 为什么 ...

随机推荐

  1. mysql文件目录详解 LINUX

    http://www.cnblogs.com/yjf512/archive/2012/12/11/2813398.html

  2. [Asp.net MVC]Bundle合并,压缩js、css文件

    摘要 在web优化中有一种手段,压缩js,css文件,减少文件大小,合并js,css文件减少请求次数.asp.net mvc中为我们提供一种使用c#代码压缩合并js和css这类静态文件的方法. 一个例 ...

  3. LR杂记-nmon+analyser监控linux系统资源

    1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...

  4. 转: gob编解码

    要让数据对象能在网络上传输或存储,我们需要进行编码和解码.现在比较流行的编码方式有JSON,XML等.然而,Go在gob包中为我们提供了另一种方式,该方式编解码效率高于JSON.gob是Golang包 ...

  5. [翻译] MZTimerLabel 用作秒表或者倒计时

    MZTimerLabel 用作秒表或者倒计时 https://github.com/mineschan/MZTimerLabel A handy class for iOS to use UILabe ...

  6. 字符函数PATINDEX()与STUFF()

    函数 PATINDEX() STUFF() 取数字 英语 汉字 更多:http://msdn.microsoft.com/zh-cn/library/ms188395.aspx PATINDEX() ...

  7. MD5 SHA1 哈希 签名 碰撞 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Spring Security OAuth2 Demo

    Spring Security OAuth2 Demo 项目使用的是MySql存储, 需要先创建以下表结构: CREATE SCHEMA IF NOT EXISTS `alan-oauth` DEFA ...

  9. 20个代码生成框架 (.NET JAVA)

    1.1 CodeSmith 一款人气很旺国外的基于模板的dotnet代码生成器 官方网站:http://www.codesmithtools.com 官方论坛:http://forum.codesmi ...

  10. 基于jQuery的Cookie操作插件--简单而又没有兼容性问题!

    在网页客户端,我们经常会遇到读取或者设置cookie的情况,如果用纯生的js我们可能会遇到一些兼容性带来的麻烦,这里给大家介绍一个比较实用jquery操作cookie的插件,插件的源代码如下: 1 2 ...