Android immersive status bar (status bar like iOS and apply uniform color style)

Source: Internet
Author: User

This feature is andorid4.4 supported, at least api19 talent enough to use. Here's how to use it, it's easy:


Add a Demo source code:

Https://github.com/ws123/StatusDemo


public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Transparent status bar        GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        Transparent navigation bar        GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);    }}


        Transparent status bar        GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        Transparent navigation bar        GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

Just add these two lines of code and you'll be able to implement an immersive notification bar. Effect



Let's show you the layout of this interface:

<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:background= "#ffffff"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <textview        android:layout_width=" match_parent "        android:layout_height=" 100DP "        android:background= "#009959"/>    <button        android:layout_width= "100DP"        android:layout_ height= "50DP"        android:background= "#ff669d"/></linearlayout>

is a vertical flow layout, but in this case, there is still a problem. I add some words to the textview. Is the green piece, we look at the effect:


You see, the text and status bar overlap together, this is certainly not possible, at this time need to add the following code:


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

<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:fitssystemwindows= "true"    android:cliptopadding= "true"    android:background= "#ffffff"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <textview        android:layout_width=" match_parent "        android:layout_height=" 100DP "        android:background= "#009959"/>    <button        android:layout_width= "100DP"        android:layout_ height= "50DP"        android:background= "#ff669d"/></linearlayout>

Everybody look at the red part. Add those two lines later. The interface will still be immersive, but the part of the status bar will not overlap, like adding padding, for example:


Let's look at the picture. A button of green TextView and red is moved down. The status bar is white. is the color of the background linearlayout. Obviously, this is not what we want, we want the status bar to be the same color as the control we put on top, the same time. The contents of the control are not the same as the status bar, in fact, it is possible to simply put the two lines of code on top of our control. The code is as follows:


<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:background= "#ffffff"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <textview        android:fitssystemwindows=" true "        android:cliptopadding=" true "        Android:layout_width= "Match_parent"        android:layout_height= "100DP"        android:background= "#009959"        android:text= "Hello. Excuse me, do you have a boyfriend? "/>    <button        android:layout_width=" 100DP "        android:layout_height=" 50DP "        Android:background= "#ff669d"/></linearlayout>
Is the two lines of red code, placed on the green TextView. Such It will be the following effect:

That's what we want.




Android immersive status bar (status bar like iOS and apply uniform color style)

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.