Solution to implement immersive status bar with multiple fragments in an activity

There is a problem in the project: an activity has multiple fragments. The immersive status bar of each fragment is different. Some are red, some are black, and some are a picture (part of the picture is displayed in the status bar), and virtual buttons (commonly known as the navigation bar) should be considered Covering the layout causes some button functions in the layout to be blocked by virtual keys. I haven't found a completely consistent solution online. Now let's talk about my own ideas. Because my knowledge is limited, I don't know if there is a better method. I hope it can be helpful to some people. The idea is as follows:

To solve the problem of covering the layout of the navigation bar of some models of mobile phones, you generally add attributes to the root layout of XML:

After adding this attribute, the layout of the activity will not be covered. However, it will lead to a problem: the previous immersive status bar can not be used. It is found that the status bar has become a transparent color. The function of Android: fitssystemwindows = "true" is to adapt the system window to your own layout. Here, all your layouts are between the status bar and the navigation bar. So immersion is gone.

The above problems are solved in two parts:

1. There are no multiple fragments in a single activity.

In the root layout of the XML layout, with the attributes above, a method is called in the oncReate method of activity. This method is to generate a rectangle which is the same size as the status bar.

Fill in whatever color it is.

The code of viewcolor class is as follows (written by a person on the Internet, but the connection has been forgotten. Say sorry to the original author):

In this way, the problems of immersive status bar and bottom navigation covering layout can be solved.

2. For multiple fragments in an activity, each fragment has a different color of the status bar, and the problem of covering the layout of the bottom navigation bar needs to be solved:

1) First, solve the problem of covering the layout of the navigation bar:

Add attributes to the root node of the activity layout:

2) Each fragment implements its own immersive status bar:

In the layout of each fragment: add attributes to the first control under the root node:

Generally, the first control bit has its own title bar relativelayout or LinearLayout, or you can add an empty view on the title. This empty view is used to fill the status bar,

For example:

The first LinearLayout is the root node. The textview below is used to fill the status bar. The LinearLayout below is my own title bar. I decide what color to fill.

If one of the fragments is a picture, you don't need this empty textview. You can directly add properties to the ImageView of the picture:

Android: fitssystemwindows = "true" to make the picture enter the status bar.

Add code to oncreateview:

In this way, multiple fragments in an activity can implement their own immersive status bar and solve the problem of the bottom navigation bar. If you need the color of the bottom navigation bar (black if not processed), you can first add code in oncreate method:

If you change the color, you can refer to the method of the status bar.

The pictures will not be uploaded. They are all pictures in the project. I didn't make a demo myself.

The above is a solution to implement the immersive status bar with multiple fragments in an activity introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>