Implementation method of Android custom status bar color consistent with app style

We know that for applications on IOS, the color of the status bar is always consistent with the color of the application title bar. The user experience is very good. Can Android? If before Android 4.4, the answer is no, but after 4.4, Google allows developers to customize the background color of the status bar, which is a good experience! If the latest version of QQ is installed on your mobile phone and your Android SDK version is 4.4 or above, you can see its effect:

There are two ways to achieve this function:

1. Set theme or custom style in XML;

In view of the various versions of SDKs of various mobile phones on the market, this method is not recommended;

2. Control in code;

You can first create a baseactivity and process it in the oncreate method:

Note that tintmanager setStatusBarTintResource(R.color.top_bg_color); The color value of this step (that is, keep your status bar color consistent with your title bar color) should be written in color XML, if you use color Prasecolor will report an error.

SystemBarTintManager. Java source code:

Quoted from: https://github.com/jgilfelt/SystemBarTint

Just copy the code into your project. Well, after these works are completed, let's see the effect:

It seems that the effect has been achieved, but after careful observation, it seems that the title bar has been raised, that is, the app interface is full screen and the status is covered on the app. Well, this is not the effect we want. How to start the interface from the lower part of the status bar? Just add a property to the outermost control of the activity layout file:

Android: fitssystemwindows = "true" is OK! See the following effect:

OK, it's done!

PS: some problems have been found during use. Using the above method is effective for a single activity, but what about the navigation page that inherits tabactivity? If the mainactivity inherits tabactivity and tab1activity, tab2activity and tab3activity are three sub items, the code for setting the status bar needs to be written in the mainactivity, while Android: fitssystemwindows = "true" needs to be written in the XML layout file of the three sub activities. There is still a problem after setting, that is, there is no problem with tab1activity on the home page after entering the application, However, tab2activity and tab3activity failed to achieve the effect. Their effect is equivalent to that without Android: fitssystemwindows = "true". At the beginning of the period, I suspected that it was the reason for the different activities, so I adjusted the position of tab1activity and tab3activity. As a result, tab3activity became normal after it became the home page, and tab1activity was abnormal again. I couldn't think of anything. Finally, I couldn't help it, In the oncreate methods of tab2activity and tab3activity, a few codes are added:

It means that first find out the height of the status bar, and then set the paddingtop value of the outermost control as the height of the status bar. The result just achieves the effect. As for why only the home page activity can achieve the effect, and the subsequent sub items can not achieve the effect, I am also depressed. Knowledgeable friends can share it!

Status bar height algorithm:

The above is the implementation method of keeping the color of Android custom status bar consistent with the app style 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
分享
二维码
< <上一篇
下一篇>>