Implementation method of immersive status bar of Android and transparency of status bar

Now more and more software begin to use the immersive status bar. Here are two ways to use the immersive status bar

be careful! The immersive status bar only supports Android 4.4 and above

Status bar: gradient on 4.4 and completely transparent on 5.0. The simulator in this paper is demonstrated for 4.4

design sketch:

be careful! Differences between the two methods:

The first is to follow the background color of the layout file of the current activity for the top bar, which is convenient to use, but there is also a problem. If there is a virtual navigation key at the bottom, the background of the navigation key is the same as that at the top, for example:

The second is displayed by setting the color of the top bar, which can solve the shortcomings of the first, such as:

The first method of use:

First, set a theme of transparent system bar style in styles.xml under the values, values-v19 and values-v21 folders, as shown in the following figure:

values/style.xml:

values-v21/style.xml:

Second, configure the activities that need immersive status bar in the manifest file and add them to the theme

Third, add "Android: fitssystemwindows =" true "to the layout file of activity. However, it is necessary to distinguish whether the background is a picture or a solid color:

1. When the background is a picture, the layout can be written as follows:

effect:

2. When the background is solid color, we need to divide the layout into Title Layout and content layout. First set the background of the root layout to the background color of the title layout, then the background color of the title layout can be used directly without setting, and finally set the background color of the content layout to white

design sketch:

Well, the above is the whole process of implementing the immersive status bar. However, it is worth noting that if we have more activities and it is troublesome to add Android: fitssystemwindows = "true" to each page, we need to change it:

Write a base class basecoloractivity.class with the following code:

Then you need to inherit the base class from the activity of the status bar:

Then you can omit the line Android: fitssystemwindows = "true" in the layout file of the activity in the status bar!

Second use method (unfinished):

Write a tool class statusbarcompat.class:

usage method:

In the current onCreate of activity, calling method StatusBarCompat.compat is OK:

If it's a little troublesome to write every activity, write a base class to complete this step:

Then the page of each activity can inherit the baseactivity!

Description of the place prompted for attention in the above code:

Two points to note when hiding the system Title:

1. Use when inheriting appcompatactivity:

2. When inheriting an activity, use:

Text related Download: Click to download the source code and APK files for free

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>