<span id="Label3"></p><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif">the traditional mobile phone status bar is shown in black strips, and some of the main interface of the mobile phone has a very obvious difference. In this way, the visual width is sacrificed to a certain extent, and the interface area becomes smaller. </span></span><br style="background-color:inherit"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif">the <span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif">immersion mode status bar and the main interface are completely integrated to make the design a different visual experience. </span></span><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif">Let's start with two graphs, and it's easy to see the difference:</span></p></p><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif"><br></span></p></p><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif"></span></p></p>Android in 4.4 added the transparent status bar and navigation bar function, based on this new feature, we can start to follow the trend, the implementation of the Android immersive status bar actually show this interface code is very simple<pre name="code" class="java"><pre name="code" class="java">/** * About Interface * * @author SuS * @time 2015.07.29 */public class Aboutactivity extends baseactivity { @Override Protec Ted void OnCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); This.setcontentview (r.layout.activity_about); Setimmerselayout (findviewbyid (r.id.common_back)); Initbackbutton (); Settitlebar (r.string.durian_about); } @Override protected void Onresume () { super.onresume (); } @Override protected void onPause () { super.onpause (); } @Override protected void OnDestroy () { super.ondestroy (); }}</pre></pre><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto">now note the Setimmerselayout () method, which is implemented in the Baseactivity</span></span><pre name="code" class="java">public class Baseactivity extends fragmentactivity {private static final String TAG = "baseactivity"; ... public void Initbackbutton () {ImageView backbutton = (ImageView) This.findviewbyid (r.id.duri.). an_back_image); Backbutton.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (View V) { Finishactivity (); } }); } protected void Setimmerselayout (view view) {if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {window window = GetWindow (); /*window.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANS lucent_status); */window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); int statusbarheight = Screenutil.getstatusbarheight (this.getbasecontext ()); view.setpadding (0, statusbarheight, 0, 0); }} Public void finishactivity () {finish (); Overridependingtransition (r.anim.push_right_in, r.anim.push_right_out); } public void Settitlebar (int id) {TextView tvname = (TextView) Findviewbyid (r.id.durian_title_text); Tvname.settext (id); }}</pre>Using Window.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANS lucent_status); or<span style="background-color:inherit"><span style="background-color:inherit"> </span></span>Window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);<br style="background-color:inherit">Can make the status bar transparent, but the key is the following two lines of code, if not the two lines, it will be<br>So what are the principles of these two lines of magic code? Let's take a look at the Getstatusbarheight method in Screenutil.<pre name="code" class="java"><pre name="code" class="java"> /** * is used to get the height of the status bar. Get (recommend This way) with the resource object * * @return Returns the pixel value of the status bar height. * /public static int getstatusbarheight (context Context) { int result = 0; int resourceId = context.getresources (). getidentifier ("status_bar_height", "dimen", "android"); If (resourceId > 0) { result = context.getresources (). getdimensionpixelsize (resourceId); } return result; }</pre></pre><br>Here is the height of the status bar, and then we can achieve the ultimate effect by setting the Padding property of the common_back, namely: view.setpadding (0, statusbarheight, 0, 0) But here we need to pay attention to the details, First of all, you should understand the meaning of padding: the inner margin so look at the Common_back layout file in Activity_about.xml We are using the include reference Common_back<br><pre name="code" class="html"><pre name="code" class="html"><include android:id= "@+id/common_back" layout= "@layout/common_back"/></pre></pre><br><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto">the layout of the Common_back is as Follows:</span></span><br><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"></span></span><pre name="code" class="html"><framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/durian_head_layout "android:layout_width=" match_parent "android:layou t_height= "wrap_content" android:background= "@color/common_top_bg" > <relativelayout Android:layout_widt h= "match_parent" android:layout_height= "51dp" > <imageview android:id= "@+id/durian_back_ima GE "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layo ut_centervertical= "true" android:layout_marginleft= "18dp" android:padding= "5dp" android:s rc= "@drawable/btn_back_selector"/> <textview android:id= "@+id/durian_title_text" Andro Id:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerinparent= "t Rue "android:textcolor=" @color/common_text_black "android:textsize=" 18sp "/> <imageview android:id=" @+id/durian_titlebar_ima Ge1 "android:layout_width=" 51DP "android:layout_height=" 51DP "android:layout_alignparentr ight= "true" android:layout_centervertical= "true" android:scaletype= "centerinside" Android : visibility= "gone"/> <imageview android:id= "@+id/durian_titlebar_image2" android:layou T_width= "51dp" android:layout_height= "51dp" android:layout_centervertical= "true" android: layout_toleftof= "@id/durian_titlebar_image1" android:scaletype= "centerinside" android:visibility= "go NE "/> </RelativeLayout></FrameLayout></pre><br>Here we use a layer of framelayout wrapped in relativelayout, here is what mystery that, in fact, here is to facilitate the above two lines of magic code to work, This allows us to set the relativelayout relative to the framelayout as the height of the status bar, which is perfect for the purpose of the immersive status bar and ensures that the relative position of the navigation bar is Constant.<br style="background-color:inherit">There is a common_back as a benchmark to control height to the height of the status bar, if an activity has only one background picture or does not use the space of the navigation bar as a benchmark, how to implement the immersive status bar, such as to achieve this state, we just need to set in the code<pre name="code" class="java"><pre name="code" class="java"> protected void Setimmerselayout (view view) { if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) { Window window = GetWindow (); /*window.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_ translucent_status); * /window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); /* int statusbarheight = screenutil.getstatusbarheight (this.getbasecontext ()); view.setpadding (0, statusbarheight, 0, 0); */ } }</pre></pre><br>Then add these two words to the current Activity's layout file<span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun"><span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun">android:fitssystemwindows= "true"</span></span><span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun"><span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun"> </span></span><span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun"><span style="background-color:rgb(245,248,253); line-height:1.5; font-family:simsun">android:cliptopadding= "true"</span></span><br style="background-color:inherit">The background of the status bar is fused with the entire background of the Activity.<br><br><p><p>Summarize:</p></p><p><p>Based on the above approach, we can implement the status bar and navigation bar, as well as the status bar and the large background of the page immersive experience.</p></p><p><p>In fact, the above can also see some of the code encapsulation techniques: such as let all of our activity inherit baseactivity, so like</p></p><p><p></p></p>Setimmerselayout (findviewbyid (r.id.common_back)); Initbackbutton (); Settitlebar (r.string.durian_about); This saves time and effort!<br>Welcome to shoot bricks, reviews!<br>Today's Blog is written here, hurry home, or outside the rain again!<br><p><p></p></p><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><p style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif"><br></span></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Immersive status bar implementation</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service