簡體   English   中英

透明狀態欄 - Android 4.4之前(KitKat)

[英]Transparent status bar - before Android 4.4 (KitKat)

我知道在Android 4.4 KitKat(API 19)中,可以使狀態欄透明。

但是,例如,Go Launcher Ex和其他人可以選擇讓它變得透明,而且它也適用於我的預裝KitKat( Android 4.3 (Jelly Bean))以及我的Android 4.0(Ice Cream Sandwich)設備。

我不需要root或任何特殊權限來使用它。

下載Go Launcher Ex並親自試用。 對我來說,如果我需要root也可以。

但是他們是怎么做到的? 如何在預KitKat設備上使Android的狀態欄保持半透明狀態?

---澄清事情---

我不是在談論行動吧 ; 我的意思是狀態欄 (通知欄)!


看這些示例圖片:

(請注意,這是從我的股票銀河筆記3中獲取的Android 4.3Go Launcher Ex 。)

(同樣適用於我的Galaxy S2Android 4.0 。)

沒有透明的狀態欄:

沒有透明條(默認)

啟用透明狀態欄:(我想要實現這個預先4.4(API 19)設備)

使用透明狀態欄(我希望以任何方式在4.4之前的設備上實現

我發現了如何在三星和索尼設備上獲得透明狀態欄
至少運行Android 4.0。

首先讓我解釋為什么在大多數設備上都不可能:
從官方方面來說,直到KitKat(4.4)才有解決方案。
但是像三星和索尼這樣的設備制造商已在其專有軟件中添加了此選項。

三星使用他們自己的皮膚版Android,名為TouchWiz,索尼也在使用他們自己的系統。
他們有自己的View類實現,帶有額外的SYSTEM_UI_標志,允許在4.4之前的設備上啟用透明狀態欄!

但證明這些標志存在並訪問它們的唯一方法是使用Reflection。
這是我的解決方案:


解析視圖標志:

int ResolveTransparentStatusBarFlag()
{
    String[] libs = getPackageManager().getSystemSharedLibraryNames();
    String reflect = null;

    if (libs == null)
        return 0;

    for (String lib : libs)
    {
        if (lib.equals("touchwiz"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
        else if (lib.startsWith("com.sonyericsson.navigationbar"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
    }

    if (reflect == null)
        return 0;

    try
    {
        Field field = View.class.getField(reflect);
        if (field.getType() == Integer.TYPE)
            return field.getInt(null);
    }
    catch (Exception e)
    {
    }

    return 0;
}

將它應用於WindowdecorView (在任何Activity中):

void ApplyTransparentStatusBar()
{
    Window window = getWindow();
    if (window != null)
    {
        View decor = window.getDecorView();
        if (decor != null)
            decor.setSystemUiVisibility(ResolveTransparentStatusBarFlag());
    }
}

我想也許有其他設備制造商已經實現了這樣的旗幟
但我只能算出這兩個(因為我擁有三星和索尼設備)

用這個。 :)

在super()之后和之前設置內容。

if (Build.VERSION.SDK_INT >= 19)
{
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21)
{
    setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}

if (Build.VERSION.SDK_INT >= 21)
{
    setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
    getWindow().setStatusBarColor(Color.TRANSPARENT);
}


public static void setWindowFlag(Activity activity, final int bits, boolean state)
{
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();

    if (state)
     {
        winParams.flags |= bits;
    }
     else
     {
        winParams.flags &= ~bits;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM