Android 沉浸式状态栏

来源:互联网 发布:java 异常处理机制 编辑:程序博客网 时间:2024/05/05 05:48

沉浸式状态的应用,是android4.4加入的功能实现。

先看看应用的实例(状态栏的颜色改变了,整个界面和谐了):

     

实现方法有三种:

1,系统实现

在代码中进行设置:

public class TopBarActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }        setContentView(R.layout.layout_topbar);    }}
在布局中加入申明:fitsSystemWindows="true" , clipToPadding="true"

<?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="fill_parent"        android:layout_height="wrap_content"        android:textSize="22sp"        android:textColor="@android:color/white"        android:background="@android:color/holo_blue_dark"        android:text="topbar"/></LinearLayout>

2,在Android4.4加入 WindowStanlucentStatus这个属性后,Android的StatusBar这个区域时可用的,于是我们可以在根布局

动态添加一个与状态栏等高的View,并设置背景色。

下面是一个工具类:

package com.example.songbinwang.liveinhand.uitls;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Build;import android.view.View;import android.view.ViewGroup;import java.lang.reflect.Field;/** * Created by songbinwang on 2016/10/20. */public class StatusBarCompat {    public static final int Color_Default = Color.parseColor("#20000000");    public static void compat(Activity activity){        compat(activity, Color_Default);    }    public static void compat(Activity activity, int statusColor){        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){            if(statusColor != -1){                activity.getWindow().setStatusBarColor(statusColor);            }        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){            if(statusColor != -1){                ViewGroup convertView = (ViewGroup) activity.findViewById(android.R.id.content);                int statusHeight = getStatusBarHeight(activity);                View statusView= new View(activity);                statusView.setBackgroundColor(statusColor);                ViewGroup.LayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusHeight);                convertView.addView(statusView, lp);            }        }    }    /**     * 获取状态栏的高度     * @return     */    protected static int getStatusBarHeight(Context context){        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  context.getResources().getDimensionPixelSize(x);        }catch(Exception e){            e.printStackTrace();        }        return 0;    }}
Activity调用(在这里我没有使用,getWindow().addFlags(int)方法,因为android5.0之后,getWidow().setStatusBarColor()相冲突):

public class TopBarActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ){            StatusBarCompat.compat(this, Color.parseColor("#ff0099cc"));        }        setContentView(R.layout.layout_topbar);    }}

在values-v19对应(Android4.4)中的styles.xml中添加属性<item name="fitsSystemWindows">true</item> 。 values-v21/styles.xml中去掉(values-21对应Android5.0)

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>        <item name="android:windowActionBar">false</item>        <item name="android:<span style="color: rgb(63, 63, 63); font-family: 'Source Code Pro', monospace; font-size: 12.6px; line-height: 35px; white-space: nowrap; background-color: rgba(128, 128, 128, 0.0745098);">windowTranslucentStatus</span>">true</item> <!--values-v19/styles.xml-->    </style>

第三种方法是一个开源库。SystemBarTint

其实它的的核心类就一个:SystemBarTint-master\library\src\com\readystatesoftware\systembartint\SystemBarTintManager.java

我们只要把这个类拷贝到你的项目中,就可以使用:

public class TopBarActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        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);            //激活导航栏            tintManager.setNavigationBarTintEnabled(true);            //设置颜色            tintManager.setStatusBarTintColor(getResources().getColor(android.R.color.holo_orange_dark));            tintManager.setNavigationBarTintColor(getResources().getColor(android.R.color.holo_green_dark));        }        setContentView(R.layout.layout_topbar);    }}

布局文件:

<?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:fitsSystemWindows="true"    android:clipToPadding="true">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="22sp"        android:textColor="@android:color/white"        android:background="@android:color/holo_blue_dark"        android:text="topbar"/></LinearLayout>

展示一下三种方法测试的效果图:图一为,第一种方法和第二种方法测试的效果图,图二为第三种方法的测试效果图。

     















0 0