Full adaptation of the transparent status bar (StatusBar)
The status bar specifies the location on the top of your Android phone to display your phone's status information.
The transparent status bar is a new feature that Android has added since 4.4, and it can be a status bar defined according to the color we want, so that the titlebar can be integrated with the status bar, adding to the sense of immersion.
obstacles encountered in the implementation
Because the status bar was added in 4.4, it was not possible to implement on a system that was 4.4 ago. Now the world of mobile phone android4.4 is not very large, so not too much influence.
There are a lot of technical blogs about the transparent status bar implementation, as well as my previous blog Transparency status bar (immersive status bar), implemented through Setup android:windowTanslucentStatus
and fitSystemWindows
implementation.
However, through the way of property settings in the use of the phenomenon of immersion is not the same, the use of transparent status bar in 5.0 and above the system is not completely transparent, will be transparent based on the addition of a layer of translucent shadow, very unfriendly. This solves the problem of adaptation between different system versions.
How to solve
For the transparent status bar, there are two different cases
- Set the status bar to a simple color.
- When the top is a picture, immerse the picture in the status bar.
For adaptation, there are two conditions that need to be adapted to match
4.4 System
5.0 and above Systems
So the idea is as follows:
Code implementation
/** * * Status bar Implementation Package class *-complete the following two functions: * 1, set a solid color, suitable for 4.4 and above, need to deal with 5.0 and above the shadow effect * 2, when top is a picture, move up to the status bar * creat Ed by mdw on 2016/5/6. */ Public class statusbarutils { /** * Set the status bar in activity in acitivity to a solid color * @param activity to be set by activity * @param color settings Color (usually titlebar color) */ Public Static void SetColor(Activity activity,intColor) {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {//5.0 and above, do not set transparent status bar, set will have translucent shadowActivity.getwindow (). Clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//Set the background color of the StatusBarActivity.getwindow (). Setstatusbarcolor (color); }Else if(Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {Activity.getwindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//Generate a rectangle with the size of the status barView Statusview = Createstatusbarview (activity, color);//Add Statusview to LayoutViewGroup Decorview = (viewgroup) Activity.getwindow (). Getdecorview (); Decorview.addview (Statusview);//Let our activity_main. Layout in XML adapts to screenSetrootview (activity); } }/** * When top department is a picture when the picture is displayed on the status bar * @param activity */ Public Static void setimage(Activity activity) {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {//5.0 and above, do not set transparent status bar, set will have translucent shadowActivity.getwindow (). Clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//Is activity_main. Images in XML can be immersed in the status barActivity.getwindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//Set the status bar color transparent. Activity.getwindow (). Setstatusbarcolor (color.transparent); }Else{//.... Activity.getwindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }/** * Set the root layout parameters to fit the layout parameters transparent status bar * */ Private Static void Setrootview(Activity activity) {//Get to Activity_main.xml fileViewGroup Rootview = (viewgroup) ((ViewGroup) Activity.findviewbyid (Android. r.id.content)). Getchildat (0);//If the parameter is not set, the content will be displayed on the status barRootview.setfitssystemwindows (true); }/** * Get the height of the status bar * @param acitivity * @return * * Private Static int Getstatusbarheight(Activity acitivity) {intResourceId = Acitivity.getresources (). Getidentifier ("Status_bar_height","Dimen","Android");returnAcitivity.getresources (). Getdimensionpixeloffset (ResourceId); }/** * Generates a rectangle bar with the same size as the status bar * * @param activity to be set for activity * @param color status bar colour value * @return status bar Rectangle Bar */ Private StaticViewCreatestatusbarview(Activity activity,intColor) {//Draw a rectangle as high as the status barView Statusbarview =NewView (activity); Linearlayout.layoutparams params =NewLinearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, getstatusbarheight (activity)); Statusbarview.setlayoutparams (params); Statusbarview.setbackgroundcolor (color);returnStatusbarview; }}
The comments are very detailed and do not explain too much.
Use
Note: The statusbarutils has been fully processed and no special processing is required in the XML file when it is used.
- Mainactivity (Sets the status bar to a solid color)
XML file
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =" match_parent " android:orientation =; <linearlayout android: Background = "#FFFF0000" android:gravity = "center" android:layout_width = "match_parent" android:layout_height =" 50DP "; <TextViewandroid:textcolor= "#fff"android:text="StatusBar" android:layout_width="Wrap_content"android:layout_height="Wrap_content" / > </linearlayout> <buttonandroid:onclick="Toimage"android:text="Jump picture" Android:layout_width="Wrap_content"android:layout_height="Wrap_content" /> </linearlayout>
Java files
@Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置状态栏为红色 StatusBarUtils.setColor(this, Color.RED); } publicvoidtoImage(View view){ new Intent(this,ImageViewTranslucentActivity.class); startActivity(intent); }
- Imageviewtranslucentactivity (top for picture)
activity_image.xml
File
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:orientation =" vertical " android:layout_width = "match_parent" android:layout_height = "match_parent" ; <ImageViewandroid:background= "#f0f"android:src="@mipmap/ic_ Launcher "android:layout_width=" Match_parent "android:layout_height=" Wrap_ Content " /> </linearlayout>
ImageViewTranslucentActivity
Java files
@Override protectedvoidonCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); //设置图片沉浸 StatusBarUtils.setImage(this); }
effect
Full adaptation of the transparent status bar (StatusBar)