The most detailed android changes the color of the status bar (task bar) to achieve the effect of the picture entering the modification bar?

I suggest that when testing the following code, create a new project by yourself and test the following code, so that the effect is clearer and avoids the effect of being careless.
Introduction: The
most important thing is to take notes, and the function is relatively simple.

The first step:
Remove the title bar and modify the style.xml to the following conditions.
Insert picture description here
Situation before modification: Situation
Insert picture description here
after modification:
Insert picture description here
You can see that the tab bar is missing. (Does it feel a bit verbose, the most important thing is for details).

The second step (emphasis, the previous is generally known, now is the key, please read carefully):
remove the color of the modified column

Window window =this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this,R.color.white));

Insert picture description here
You can see that there is a window.setStatusBarColor in the picture with a red place. It should be a version issue. Use Alter+enter to select the first one. After selecting, the circle in the figure below will appear, and the red is gone.
Insert picture description here
In the red circle in the following figure, modify it to the color you need. Here we modify it to white, which is modified in the color.xml file.
Insert picture description here
Insert picture description here
Then we run it first.
We can see that the interface has all become white, because the icon of the status bar used to be white, because we set it here also white, so we will find that the icon of the status bar will not be clear, so is it very unsightly? Let me talk about how to modify the color of the icon.
Insert picture description here
Step 3:
Modify the color code of the status bar icon
as follows:

 public void changStatusIconCollor(boolean setDark) {
    
    
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    
    
            View decorView = getWindow().getDecorView();
            if(decorView != null){
    
    
                int vis = decorView.getSystemUiVisibility();
                if(setDark){
    
    
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else{
    
    
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

As shown in the figure, write this method, but don't forget to call this method. When true , set the status bar icon color to black, and false to set the status bar color to white.
Insert picture description here
The effect is shown in the figure:
Insert picture description here
here the effect is complete

Extend function
First, take a look at the effect of the code we wrote just now, we can see that the effect we want is not achieved. Here I found a picture with more obvious effect (manually funny). This test is possible. You can directly find the picture and try it. I am too long-winded, so I won't go into details here.
Insert picture description here
First, delete the code in the red circle that we just wrote in the figure.
Insert picture description here
Second, add the code in the blue circle. The code
Insert picture description here
is as follows:

 if (Build.VERSION.SDK_INT >= 21) {
    
    
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

Then we look at the effect.
Insert picture description here
It happens to be the effect I want. Here we can modify the status bar. Isn't it much better?

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/110503269