android 沉浸式状态栏-4.4版本qq

来源:互联网 发布:以上内容来源于网络 编辑:程序博客网 时间:2024/05/24 06:56

1. 前言

沉浸式模式(Immersive Mode)大家应该都非常熟悉了,其实说白了,就是全屏显示。类似qq的界面如图所示:
这里写图片描述
注意:这个特性是andorid4.4支持的,最少要api19才可以使用.

2.使用方法

仅仅只需要几行代码就能完全实现沉浸式模式:
(1)activity中添加如下代码:

 @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (Build.VERSION.SDK_INT >= 19) {            //透明状态栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            //透明导航栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }    }

(2)xml文件中加入如下代码:

android:fitsSystemWindows="true"  android:clipToPadding="true" 

这段代码有两个作用:1.解决文字和状态栏重叠在一起。2.决定状态栏颜色
(3)状态栏颜色

<?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="#123456"    android:orientation="vertical"    android:fitsSystemWindows="true"    android:clipToPadding="true"    tools:context="com.example.test.test.MainActivity">    <TextView        android:text="Hello World!"        android:background="#666666"        android:layout_width="wrap_content"        android:layout_height="100dp" />    <TextView        android:text="Hello World!"        android:background="#987654"        android:layout_width="wrap_content"        android:layout_height="100dp" /></LinearLayout>

状态栏将和背景一个颜色,如下图所示:
这里写图片描述


把那两行代码放到我们顶部的控件就可以实现状态栏颜色跟顶部控件颜色一致,如图所示:

<?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="#123456"    android:orientation="vertical"    tools:context="com.example.test.test.MainActivity">    <TextView        android:fitsSystemWindows="true"        android:clipToPadding="true"        android:text="Hello World!"        android:background="#666666"        android:layout_width="match_parent"        android:layout_height="100dp" />    <TextView        android:text="Hello World!"        android:background="#987654"        android:layout_width="match_parent"        android:layout_height="100dp" /></LinearLayout>

这里写图片描述

3.一些坑

(1)状态栏背景色
上面的FLAG_TRANSLUCENT_STATUS 只是把状态栏设置为透明的,但是,状态栏是有背景色的,一些手机的状态栏背景色为透明,而一些手机的状态栏背景色为半透明的黑色.于是在5.0上增加了WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 和 getWindow().setStatusBarColor(int color),在5.0才能看到清爽的全透状态栏。使用方法如下:

// 部分机型的statusbar会有半透明的黑色背景getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);getWindow().setStatusBarColor(Color.TRANSPARENT);// SDK21

(2)状态栏字体颜色
在6.0增加了View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,这个字段就是把状态栏标记为浅色,然后状态栏的字体颜色自动转换为深色。所以,如果需要浅色的状态栏,只能在Android6.0及以后的版本中实现。

if (Build.VERSION.SDK_INT >= 23) {                win.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏                // 状态栏字体设置为深色,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 为SDK23增加                win.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);            }

4.demo下载地址如下

demo下载地址

0 0
原创粉丝点击