Android Completely transparent Status Bar?

前端 未结 28 2727
旧时难觅i
旧时难觅i 2020-11-22 14:10

I\'ve searched the documentation but only found this: Link. Which is used to make the bar translucent? What I\'m trying to do is to make t

相关标签:
28条回答
  • 2020-11-22 14:25

    THERE ARE THREE STEPS:

    1) Just use this code segment into your OnCreate method

      // FullScreen
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
      WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    

    If you’re working on Fragment, you should put this code segment in your activity’s OnCreate method.

    2) Be sure to also set the transparency in /res/values-v21/styles.xml:

    <item name="android:statusBarColor">@android:color/transparent</item>
    

    Or you can set the transparency programmatically:

    getWindow().setStatusBarColor(Color.TRANSPARENT);
    

    3) In anyway you should add the code segment in styles.xml

    <item name="android:windowTranslucentStatus">true</item>
    

    NOTE: This method just works on API 21 and above.

    0 讨论(0)
  • 2020-11-22 14:25

    in my case, i dont call at all "onCreate" (its a react native app and this can be fixes also by using the react-native StatusBar component) one can also use this:

    override fun onStart() {
            super.onStart()
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
    }
    
    0 讨论(0)
  • 2020-11-22 14:25

    This is only for API Level >= 21. It works for me. Here is my code (Kotlin)

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            findViewById<View>(android.R.id.content).systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }
    
    0 讨论(0)
  • 2020-11-22 14:27

    Works for Android KitKat and above (For those who want to transparent the status bar and don't manipulate the NavigationBar, because all of these answers will transparent the NavigationBar too!)

    The easiest way to achieve it:

    Put these 3 lines of code in the styles.xml (v19) -> if you don't know how to have this (v19), just write them in your default styles.xml and then use alt+enter to automatically create it:

    <item name="android:windowFullscreen">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:fitsSystemWindows">false</item>
    

    And now, go to your MainActivity Class and put this Method out of onCreate in the class:

    public static void setWindowFlag(Activity activity, final int bits, boolean on) {
    
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
    

    Then put this code in the onCreate method of the Activity:

    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
        }
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        //make fully Android Transparent Status bar
        if (Build.VERSION.SDK_INT >= 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    

    That's it!

    0 讨论(0)
  • 2020-11-22 14:28

    Here is a extension in kotlin that do the trick:

    fun Activity.setTransparentStatusBar() {
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = Color.TRANSPARENT
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:29

    Simple and crisp and works with almost all use cases (for API level 16 and above):

    1. Use the following tag in your app theme to make the status bar transparent:

      <item name="android:statusBarColor">@android:color/transparent</item>

    2. And then use this code in your activity's onCreate method.

      View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
      

    That's all you need to do ;)

    You can learn more from the developer documentation. I'd also recommend reading this blog post.

    KOTLIN CODE:

        val decorView = window.decorView
        decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    

    Check my another answer here

    0 讨论(0)
提交回复
热议问题