Android 沉浸式狀態欄

沉浸式狀態的應用,是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>

展示一下三種方法測試的效果圖:圖一爲,第一種方法和第二種方法測試的效果圖,圖二爲第三種方法的測試效果圖。

     















發佈了98 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章