Transparent status bar - before Android 4.4 (KitKat)

前端 未结 2 510
梦毁少年i
梦毁少年i 2020-12-04 16:16

I know in Android 4.4 KitKat (API 19) it\'s possible to make the status bar transparent.

But for example, Go Launcher Ex and others have an option to make it transpa

相关标签:
2条回答
  • 2020-12-04 16:29

    I discovered how to get a transparent status bar on Samsung and Sony devices
    running at least 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).
    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.
    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!

    But the only way to prove that those flags are existing and to access them is using 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):

    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)

    0 讨论(0)
  • 2020-12-04 16:33

    Use this. :)

    after super() and before set content.

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题