简体   繁体   English

透明状态栏 - Android 4.4之前(KitKat)

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

I know in Android 4.4 KitKat (API 19) it's possible to make the status bar transparent. 我知道在Android 4.4 KitKat(API 19)中,可以使状态栏透明。

But for example, Go Launcher Ex and others have an option to make it transparent, and it's working on pre KitKat also, for me ( Android 4.3 (Jelly Bean)) too and also on my Android 4.0 (Ice Cream Sandwich) device. 但是,例如,Go Launcher Ex和其他人可以选择让它变得透明,而且它也适用于我的预装KitKat( Android 4.3 (Jelly Bean))以及我的Android 4.0(Ice Cream Sandwich)设备。

I didn't need root or any special privileges to use it. 我不需要root或任何特殊权限来使用它。

Download Go Launcher Ex and try it yourself. 下载Go Launcher Ex并亲自试用。 For me it would also be OK if I would need root. 对我来说,如果我需要root也可以。

But how did they do that? 但是他们是怎么做到的? How can I make Android's status bar translucent on pre-KitKat devices? 如何在预KitKat设备上使Android的状态栏保持半透明状态?

---To clarify things--- ---澄清事情---

I'm not talking about the Action Bar ; 我不是在谈论行动吧 ; I mean the Status Bar (Notification Bar)! 我的意思是状态栏 (通知栏)!


see these example pictures: 看这些示例图片:

(Notice, this is taken from my Stock Galaxy Note 3 with Android 4.3 and Go Launcher Ex .) (请注意,这是从我的股票银河笔记3中获取的Android 4.3Go Launcher Ex 。)

(The same thing works with my Galaxy S2 and Android 4.0 too.) (同样适用于我的Galaxy S2Android 4.0 。)

Without a transparent status bar: 没有透明的状态栏:

没有透明条(默认)

With transparent status bar enabled: (I want to achieve this on pre 4.4 (API 19) devices) 启用透明状态栏:(我想要实现这个预先4.4(API 19)设备)

使用透明状态栏(我希望以任何方式在4.4之前的设备上实现

I discovered how to get a transparent status bar on Samsung and Sony devices 我发现了如何在三星和索尼设备上获得透明状态栏
running at least Android 4.0. 至少运行Android 4.0。

To start off let me explain why it isn't possible on most devices: 首先让我解释为什么在大多数设备上都不可能:
From the official side of Android, there is no solution until KitKat (4.4). 从官方方面来说,直到KitKat(4.4)才有解决方案。
But some device manufacturers like Samsung and Sony have added this option in their proprietary software. 但是像三星和索尼这样的设备制造商已在其专有软件中添加了此选项。

Samsung uses their own skinned version of Android, called TouchWiz, and Sony is using their own system too. 三星使用他们自己的皮肤版Android,名为TouchWiz,索尼也在使用他们自己的系统。
They have a own implementation of the View class with additional SYSTEM_UI_ flags that allow to enable a transparent statusbar on pre 4.4 devices! 他们有自己的View类实现,带有额外的SYSTEM_UI_标志,允许在4.4之前的设备上启用透明状态栏!

But the only way to prove that those flags are existing and to access them is using Reflection. 但证明这些标志存在并访问它们的唯一方法是使用Reflection。
Here is my solution: 这是我的解决方案:


Resolve the view flag: 解析视图标志:

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;
}

Apply it on the decorView of your Window (inside any Activity): 将它应用于WindowdecorView (在任何Activity中):

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

I think there are maybe other device manufacturers that have implemented such a flag 我想也许有其他设备制造商已经实现了这样的旗帜
but I was only able to figure this two out (because I own a Samsung and a Sony device) 但我只能算出这两个(因为我拥有三星和索尼设备)

Use this. 用这个。 :) :)

after super() and before set content. 在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