Android Completely transparent Status Bar?

前端 未结 28 2728
旧时难觅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:22

    Here is the Kotlin Extension:

    fun Activity.transparentStatusBar() {
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            window.statusBarColor = Color.TRANSPARENT
    
        } else
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:23

    All one need is to go into MainActivity.java


    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Window g = getWindow();
            g.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    
            setContentView(R.layout.activity_main);
    
        }
    
    0 讨论(0)
  • 2020-11-22 14:23

    This should work

    // In Activity's onCreate() for instance

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    
    0 讨论(0)
  • 2020-11-22 14:23

    add these lines into your Activity before the setContentView()

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    

    add these 2 lines into your AppTheme

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

    and last thing your minSdkVersion must b 19

    minSdkVersion 19
    
    0 讨论(0)
  • 2020-11-22 14:24

    I found the answer while I investigating this library: https://github.com/laobie/StatusBarUtil

    so you need to add following codes to your activity

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        window.statusBarColor = Color.TRANSPARENT
    } else {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
    
    0 讨论(0)
  • 2020-11-22 14:25

    All you need to do is set these properties in your theme:

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

    Your activity / container layout you wish to have a transparent status bar needs this property set:

    android:fitsSystemWindows="true"
    

    It is generally not possible to perform this for sure on pre-kitkat, looks like you can do it but some strange code makes it so.

    EDIT: I would recommend this lib: https://github.com/jgilfelt/SystemBarTint for lots of pre-lollipop status bar color control.

    Well after much deliberation I've learned that the answer to totally disabling the translucency or any color placed on the status bar and navigation bar for lollipop is to set this flag on the window:

    // In Activity's onCreate() for instance
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    

    No other theme-ing is necessary, it produces something like this:

    enter image description here

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