Immerse yourself in the status bar with toolbar and custom navigation bars, respectively

Source: Internet
Author: User

First, ToolBar

1. Add dependencies in Build.gradle, for example:

Compile ' com.android.support:appcompat-v7:23.4.0 '

2, remove the application of the Actionbar. It is possible to modify the subject theme to "Noactionbar", for example:

<name= "Apptheme"  parent= "Theme.AppCompat.Light.NoActionBar"  >

Or do not modify the theme to "Noactionbar", and under the theme style, add:

    <name= "Windownotitle">true</item>     <name= "Windowactionbar">false  </Item>

The second property represents whether Actionbar is substituted for titlebar.

In fact, just learned when, feel very puzzled, how many more a titlebar? After a long time to find out, 3.0 ago, the status bar below is the title bar (only show a small amount of information such as title), 3.0 later became the application bar, that is, Actionbar.

In addition, when I test, the activity is inherited from Appcompatactivity, the theme is AppCompat type. In this case, you must write as above to have effect, write less or value different, either no effect, or error.

Finally, a description of the above two properties is available on Android. R.attr is viewed in this class.

3. Adding attributes to toolbar in XML

    Android:fitssystemwindows= "true"    android:minheight= "? Attr/actionbarsize"

Fitssystemwindows is the key to toolbar's immersive status bar, presumably if set to true, it will adjust the view to leave some space for the system window, if not set or set to False,toolbar will overlap with the status bar.

In the second property, its value is all written as "? Android:attr/actionbarsize", which means referencing the Actionbarsize attribute in the current topic. For more information, see the Accessing Resources section of the official documentation.

The above two properties can be viewed in the Android.view.View class.

4, add the code to judge the SDK version in Java and set the status bar to be transparent when the user's system is 4.4 and above

        if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {            GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }

Whether it's a toolbar or a custom navigation bar, this is the key to implementing an immersive status bar.

Because setting the status bar is transparent to this property, more than 4.4 can be used, so the system below 4.4 is not able to implement the immersive status bar. In a system from 4.4 to 5.0, the status bar is fully transparent, which is the same color as your toolbar and custom navigation bar. In more than 5.0 of the system, it is translucent, it will look darker.

And I in 6.0 of the system test, found that this step is not set and set, from the effect, the difference is not set when the status bar color lighter, and toolbar padding top for 0, and set the color is deep, padding top for the height of the status bar. It's not clear what the effect is. However, this distorts the appearance of the custom navigation, which increases the height of the status bar, but does not overlap the status bar, causing the effect to deform.

5. Finally add in Java

Setsupportactionbar (Mtoolbar);

Layout Code for toolbar:

<?XML version= "1.0" encoding= "Utf-8"?><Android.support.v7.widget.Toolbarxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/tool_bar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"android:elevation= "4DP"android:fitssystemwindows= "true"Android:minheight= "? Android:attr/actionbarsize"App:title= "ToolBar"App:subtitle= "Toolbar"/>

(Android 6.0):

Second, custom navigation bar Topbar

1, the setting window is untitled, the 2nd step in the above two methods can be implemented, or in Java add the following code:

        Requestwindowfeature (Window.feature_no_title);

Note that when you add this code, make sure that it is before the layout content is loaded, that is, before the OnCreate Setcontentview.

In addition, I found that if the activity is inherited appcompatactivity words, only write the above code is unchanged, the display is still actionbar. But if it is inherited fragmentactivity, it will have effect, it is said in the 2nd step above the second method, only add any one of the properties are possible. As for what the reason is, I haven't figured it out yet.

2, with the 4th step above, judge the system version and set the status bar to be transparent on demand

3. Get the height of the status bar

    protected int Getstatusheight () {        try {            class<?> c = class.forname ("Com.android.internal.r$dimen"); Gets the class object that corresponds to the string, a            . C.newinstance ();//Create an instance object of this class            field field = C.getfield ("Status_bar_ Height "); Get the string corresponding to the variable            int x = Integer.parseint (Field.get (object). toString ());//Through this instance object to get the value of this variable, then convert the type, and finally to the integer, into a resource ID            return Getresources (). getdimensionpixelsize (x);        } catch (Exception e) {            e.printstacktrace ();        }        return 0;    }

This part of the code is implemented using the reflection mechanism of Java, because the internal package is removed by default by the Android.jar in the sdk/platforms/android-version, so it is not possible to invoke or view the classes in this package directly. If you want to use it, you can take advantage of this open source project Https://github.com/anggrayudi/android-hidden-api.

4. Get the height of the custom Topbar and modify the layout parameters

    protected void Setstatusbar () {        if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {            final ViewGroup ViewGroup = (viewgroup) Findviewbyid (R.id.top_bar);            Final int statusheight = Getstatusheight ();            Viewgroup.post (New Runnable () {                @Override public                void Run () {                    int topbarheight = Viewgroup.getheight ();                    Linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) viewgroup.getlayoutparams ();                    Layoutparams.height = Statusheight + topbarheight;                    Viewgroup.setlayoutparams (Layoutparams);}}            );        }    }

Because in the Include this Topbar layout file, its parent layout is linearlayout, and Topbar's parent layout is relativelayout, so here first to turn into ViewGroup, and so on Getlayoutparams, and turn it into linearlayout.layoutparams.

Layout of Topbar:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/top_bar"Android:layout_width= "Match_parent"Android:layout_height= "49DP"Android:background= "@color/colorprimary"android:gravity= "Bottom">        <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "49DP">        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:text= "@string/app_name"android:textsize= "24SP"Android:textcolor= "#ffffff"/>    </Relativelayout></Relativelayout>

Because the height of this layout is dynamically modified in the code, that is, the height of the 49DP plus the status bar, so there is only one level of structure, the content of the navigation bar will be upward bias. Therefore, to nest more than one layer to maintain the height of the navigation bar, while in the outermost layout, add android:gravity= "Bottom" this property to ensure that the navigation bar is not upward bias.

(Android 6.0):

  

  

  

Immerse yourself in the status bar with toolbar and custom navigation bars, respectively

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.