美文网首页Android
实现透明状态栏的方法

实现透明状态栏的方法

作者: Mr_王大锤 | 来源:发表于2016-09-01 14:07 被阅读0次

之前一直对透明状态栏的设置有一些疑问,在经过查找相关的资料后,对透明状态栏的使用有了一定的理解。在这里写下来,方便自己日后查看。
在这里给大家一个友情提示,如果你是用真机调试的,请先在设置—>通知和状态栏中,将沉浸式状态栏关闭(魅族),不然你会发现即使没有使用下面这些方法,也实现了透明状态栏的效果。这一点让我纳闷了半天…

在代码中实现透明状态栏

以前在看资料时,经常会看到“沉浸式状态栏”一词,但是一直不知道什么什么样的才能称为沉浸式状态栏,直到最近看了郭神的博客,感觉到豁然开朗,Android沉浸式模式的本质就是全屏化,将屏幕控件都用来显示内容。所以没有所谓的沉浸式状态栏这种叫法,只能算是透明状态栏吧。

类似于这种效果

透明状态栏效果

(1)首先,通过代码实现全屏显示,实现下图的效果。

全屏显示效果

在Activity的onCreate方法中写如下代码

View decorview = getWindow().getDecorView();
//1.全屏状态
int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorview.setSystemUiVisibility(option);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();//隐藏actionbar

这里通过getWindow().getDecorView()这个方法得到当前界面的DecorView,然后通过setSystemUiVisibility()来设置系统UI的可见性,View.SYSTEM_UI_FLAG_FULLSCREEN的意思是全屏显示,就是将系统的状态栏隐藏。根据Android的设计建议,ActionBar不应该独立于状态栏显示,而且隐藏状态栏,显示ActionBar也很丑,所以这里需要将ActionBar隐藏。

(2)实现透明状态栏的效果。

透明状态栏效果

实现代码

View decorview = getWindow().getDecorView();
if(Build.VERSION.SDK_INT>=21){//5.0以上的系统支持    
    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |View.SYSTEM_UI_FLAG_LAYOUT_STABLE;//表示让应用主题内容占据系统状态栏的空间        
   decorview.setSystemUiVisibility(option); 
   getWindow().setStatusBarColor(Color.parseColor("#00ffffff"));//设置状态栏颜色为透明    
   ActionBar actionBar  = getSupportActionBar();   
   actionBar.hide();
 }

通过View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE设置页面布局的全屏显示,并显示状态栏,这两个必须一起设置,表示让页面的主题内容占用系统状态栏的空间,再通过getWindow().setStatusBarColor(Color.parseColor("#00ffffff"))设置状态栏的颜色,但是这个方法只有5.0及以上的系统才支持,所以需要做一个判断。

(3)实现隐藏导航栏

隐藏导航栏

实现代码

int option = View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorview.setSystemUiVisibility(option);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();

我使用的是魅族手机,没有导航栏,所以看不到效果,原来效果是和图(1)相同的,但是点击屏幕后,状态就出来了,并且不会隐藏,也就是退出了全屏模式。

(4)透明导航栏和状态栏

透明状态栏和导航栏

实现代码

if(Build.VERSION.SDK_INT>=21){
    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
    decorview.setSystemUiVisibility(option);
    getWindow().setStatusBarColor(Color.TRANSPARENT);          
    getWindow().setNavigationBarColor(Color.TRANSPARENT);
    ActionBar actionBar = getSupportActionBar();    
    actionBar.hide();
}

只有5.0及以上的系统才能通过这种方法设置。

(5)真正的沉浸式模式,使用整个屏幕显示内容

沉浸式模式

实现代码

@Override
public void onWindowFocusChanged(boolean hasFocus) {  
super.onWindowFocusChanged(hasFocus);    
if (hasFocus && Build.VERSION.SDK_INT >= 19) {       
     View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
            View.SYSTEM_UI_FLAG_FULLSCREEN |
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);    
    }
}

重写Activity的onWindowFocusChanged方法实现沉浸模式,只有4.4及以上系统支持沉浸式模式。

设置主题实现透明状态栏。

Android从4.4开始引入了Translucent System Bar特性,即半透明状态栏,透过这个特性可以比较容易的在4.4及以上系统中实现透明状态栏效果。由于4.4和5.0设置方式不同,所以需要在res目录下,建立values,values-v19,values-v21三个目录,每个目录中建立styles.xml样式文件。这样不同的系统就会自动用相应的样式了。

目录结构

透明状态栏有两种不同的场景,一种是图片背景,一种是纯色背景。

QQ音乐纯色背景
首先来说一下图片当做背景的使用场景,在三个目录中的styles.xml中分别定义ImageTranslucentBarTheme主题。

values目录中,针对4.4以前的系统

<style name="ImageTranslucentBarTheme" parent="AppTheme">   
 <!--4.4之前的状态栏跟随系统-->
</style>

values-v19,针对4.4以后的系统

<style name="ImageTranslucentBarTheme"     parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!--半透明状态栏-->    
    <item name="android:windowTranslucentStatus">true</item>
    <!--半透明导航栏-->  
    <item name="android:windowTranslucentNavigation">true</item>
</style>

values-v21,针对5.0以后的系统,需要注意的是,在5.0以后的系统中,需要手动设置状态栏的颜色,若没有设置,则状态栏是半透明的白色。

<style name="ImageTranslucentBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">    
    <!--半透明状态栏-->    
    <item name="android:windowTranslucentStatus">true</item>
    <!--半透明导航栏-->
    <item name="android:windowTranslucentNavigation">true</item>
    <!--5.0以后要手动设置状态栏的颜色-->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

在layout中,需要在根视图中设置下面这个属性。

 android:fitsSystemWindows="true"  

在Activity中需要隐藏标题栏,因为我的Activity是继承的AppcompatActivity的,所以要这样写

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
ImageTranslucentBarTheme主题效果
另一种是纯色背景,在三个目录中的styles.xml中分别定义ColorTranslucentBarTheme主题。
values目录中,针对4.4以前的系统
<style name="ColorTranslucentBarTheme" parent="AppTheme">
    <!--4.4之前的状态栏跟随系统-->
</style>

values-v19,针对4.4以后的系统

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

values-v21,针对5.0以后的系统,需要注意的是,在5.0以后的系统中,需要手动设置状态栏的颜色,并且需要把layout中标题栏视图的背景颜色设置的和状态栏一样,和上一个方法一样,在根视图中加入*android:fitsSystemWindows="true"
*属性

<style name="ColorTranslucentBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">#31c27c</item>
</style>

布局文件

<?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"
   android:fitsSystemWindows="true"
   tools:context="com.wang.administrator.statubar.ColorTranslucentActivity">
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="55dp"
          android:background="#31c27c">
              <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="QQ Music"
                    android:textColor="@android:color/white"
                    android:layout_centerInParent="true"
                    android:textSize="20sp"/>
      </RelativeLayout>
      <Button
            android:id="@+id/show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="Show A Toast"/>
 </LinearLayout>
ColorTranslucentBarTheme主题效果

第一次写笔记,有说的不对的地方欢迎指出。
参考连接
Translucent System Bar 的最佳实践
Android沉浸式状态栏完全解析

相关文章

网友评论

    本文标题:实现透明状态栏的方法

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