Android three sentence code using immersive status bar

Source: Internet
Author: User


Anyone who has ever used an Android phone knows that the status bar at the top of the screen is black when Android is using the app, even if it's not black, unlike the app color that's being opened. There is a feeling of gray often does not adjust. ~

Today, I took a look at the information about the immersive status bar ~ ~ How can a patient with severe obsessive-compulsive disorder be missed?

Start using the immersive status bar tour below:

Code not in advance:

Immersion style:


Non-immersive:


Well, although the second picture is blurry, you can imagine the first one looks better (~ ̄▽ ̄).

So the next thing to say is how to achieve the effect of the first picture:

The first thing to note is that the following methods are only suitable for android4.4 or above systems

android4.4 is api19~ so we create a new values-v19 under the Res folder, and then create a new Style.xml file.

Write the following code on the style:

Specifies that the style is noactionbar and translucent

<resources xmlns:android= "Http://schemas.android.com/apk/res/android" >    <style name= "Appbasetheme" Parent= "Android:Theme.Holo.Light.NoActionBar.TranslucentDecor" >    </style></resources>

Then run the program to see that the status bar is the same as the top color of the app, but if something else is written at the top of the layout file, you'll see that the content on the layout file is coincident with the contents of the status bar ~ ~ This is definitely not allowed.

Is there a way to solve it?

Write the following two sentences on the layout file using the immersive status bar:


Then write two lines of code on the layout file where you want to use the immersive status bar:

  Android:cliptopadding= "true"  android:fitssystemwindows= "true"

can solve this problem ~

However, in some cases it will be found that after writing these two lines, the status bar color will be white or other color ~ this is because the color of the place where the two lines of code is written is the same as the status bar color.

In other words, if there is a linearlayout background color is red, then using the Immersion status bar, the LinearLayout after writing the above two lines of code, the status bar will also turn red.



Can see actually three lines of code ~

<style name= "Appbasetheme" parent= "Android:Theme.Holo.Light.NoActionBar.TranslucentDecor" >
Android:cliptopadding= "true"  android:fitssystemwindows= "true"

Finally, say:

There's so much activity in every app. If each activity is written once, the two lines are a bit annoying, so in some cases it can be "lazy". For example, many Apps use toolbar or Actionbar.

Then the color of the status bar will be the same as the bar color on the app. Then we just need to define a actionbar and then put the above two lines of code on the layout then use the activity of this custom Actionbar naturally the two code will take effect!

Here's an example:

The first is the custom Actionbar layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/rellay_action_bar "android:layout_width=" match_parent "android:layout_height=" Wrap_cont Ent "android:background=" @color/maincolor "android:cliptopadding=" true "android:fitssystemwindows=" true ">& Lt     Relativelayout android:layout_width= "match_parent" android:layout_height= "@dimen/top_navigation_height" > <imagebutton android:id= "@+id/btn_action_bar_left" android:layout_width= "24DP" Android:layout_hei ght= "24DP" android:layout_alignparentleft= "true" android:layout_centervertical= "true" Android:layout_ marginleft= "10DP" android:background= "@drawable/common_back_btn_bg_pressed" android:contentdescription= "@nul L "android:padding=" 20DP "android:visibility=" Gone "/> <textview android:id=" @+id/tv_action_ Bar_title "Android:layoUt_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" a Ndroid:layout_centerinparent= "true" android:layout_centervertical= "true" android:gravity= "center" and Roid:singleline= "true" android:textcolor= "#ffffff" android:textsize= "20sp"/> <imagebutton A Ndroid:id= "@+id/btn_action_bar_right" android:layout_width= "24DP" android:layout_height= "24DP" Androi        D:layout_alignparentright= "true" android:layout_centervertical= "true" android:layout_marginright= "10DP" android:contentdescription= "@null" android:padding= "20DP" android:visibility= "Gone"/></relativelay Out> </RelativeLayout>

Then there is the layout that uses this custom actionbar:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:orientation=" vertical "    android:layout_height= "Wrap_content" >    <com.javy.test.view.customactionbar        android:id= "@+id/action_bar_act_detail_intro "        android:layout_width=" match_parent "        android:layout_height=" wrap_content ">    </ Com.javy.test.view.customactionbar></linearlayout>


That's ~~ok.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android three sentence code using immersive status bar

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.