Android Completely transparent Status Bar?

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

    While all the answers above circulate around the same fundamental idea and you can get it to work with simple layouts using one of the examples above. However I wanted to change the color of the background while using sliding 'full screen' (tab bar aside) fragment navigation and maintain regular navigation, tab and action bars.

    After reading carefully an article by Anton Hadutski I had better understanding what is going on.

    I have DrawerLayout with ConstraintLayout (i.e. container) which has Toolbar, include for the main fragment and BottomNavigationView.

    Setting DrawerLayout having fitsSystemWindows to true isn't sufficient, you need to set both DrawerLayout and ConstraintLayout. Assuming transparent status bar, the status bar color is now the same as the background color of ConstraintLayout.

    However, the included fragment has still the status bar inset, so animating another 'full screen' fragment on top of with doesn't change the color of the status bar.

    A bit of code from the referred article into Activity's onCreate:

    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container)) { view, insets ->
            insets.replaceSystemWindowInsets(
                    insets.systemWindowInsetLeft,
                    0,
                    insets.systemWindowInsetRight,
                    insets.systemWindowInsetBottom
            )
        }
    

    And all is good, except now the Toolbar doesn't address the status bar height. Some more referring to the article and we have a fully working solution:

    val toolbar = findViewById(R.id.my_toolbar)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container)) { view, insets ->
            val params = toolbar.layoutParams as ViewGroup.MarginLayoutParams
            params.topMargin = insets.systemWindowInsetTop
            toolbar.layoutParams = params
            insets.replaceSystemWindowInsets(
                    insets.systemWindowInsetLeft,
                    0,
                    insets.systemWindowInsetRight,
                    insets.systemWindowInsetBottom
            )
        }
    

    The main_activity.xml (please note that marginTop in Toolbar is for preview purposes, it will be replaced by the code):

    
        
    
        
    
            
                ...
            
    
            
            ...
        
        ...
    
    

提交回复
热议问题