The immersive status bar in Android is implemented in the following way
The relevant API methods for calculating the height of the status bar and calling the immersive status bar
1 PackageCom.example.status;2 ImportAndroid.annotation.TargetApi;3 Importandroid.app.Activity;4 ImportAndroid.content.Context;5 ImportAndroid.os.Build;6 ImportAndroid.view.View;7 ImportAndroid.view.Window;8 ImportAndroid.view.WindowManager;9 Public classImmersedstatusbarutils {Ten One /** A * in {@linkActivity#setcontentview} is called after - * - * @paramActivity the * The activity of the immersive status bar to be implemented - * @paramTitleviewgroup - * ViewGroup of the header control, if NULL, the entire interface will overlap the status bar - */ + @TargetApi (build.version_codes. KITKAT) - Public Static voidInitaftersetcontentview (activity activity, + View Titleviewgroup) { A if(Activity = =NULL) at return; - if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) { -window window =Activity.getwindow (); - window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); - } - if(Titleviewgroup = =NULL) in return; - //sets the paddingtop of the head control ViewGroup to prevent the interface from overlapping the status bar to intStatusbarheight =getstatusbarheight (activity); +Titleviewgroup.setpadding (0, statusbarheight, 0, 0); - the } * $ /**Panax Notoginseng * Get status bar height - * the * @paramContext + * @return A */ the Private Static intGetstatusbarheight (Context context) { + intresult = 0; - intResourceId =context.getresources (). Getidentifier ( $"Status_bar_height", "Dimen", "Android"); $ if(ResourceId > 0) { -result =context.getresources (). Getdimensionpixelsize (resourceId); - } the returnresult; - }Wuyi}
Invoke the instance, using the related method written in step one.
1 PackageCom.example.status;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.Menu;6 ImportAndroid.view.View;7 ImportAndroid.view.Window;8 9 Public classMainactivityextendsActivity {Ten One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - requestwindowfeature (window.feature_no_title); the Setcontentview (r.layout.activity_main); -View TopView =Findviewbyid (R.id.lin); -Immersedstatusbarutils.initaftersetcontentview ( This, TopView); - } + - @Override + Public BooleanOncreateoptionsmenu (Menu menu) { A //inflate the menu; This adds items to the action bar if it is present. at getmenuinflater (). Inflate (R.menu.main, menu); - return true; - } - -}
The corresponding layout file
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<LinearLayout8Android:id= "@+id/lin"9Android:layout_width= "Match_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:background= "#ff123654" > A -<TextView -Android:id= "@+id/title" theAndroid:layout_width= "Match_parent" -android:layout_height= "45.0DP" -Android:background= "#ff123654" -android:gravity= "Center" +android:text= "I am the head control" -Android:textcolor= "#ffffffff"/> +</LinearLayout> A at</LinearLayout>
Immersive status bar implementation in Android