Android Development set immersive title bar

Source: Internet
Author: User

Tip: Android does not fit into the immersive title bar after version 4.4, to use this requirement

Method One:

Step one: Define a tool class Viewcolor

Package Com.contentprovide.liuliu.music_play3;import Android.annotation.targetapi;import android.app.Activity; Import Android.os.build;import android.view.view;import android.view.viewgroup;import Android.view.WindowManager; Import android.widget.linearlayout;//Set the immersive title bar of the tool class public class Viewcolor {/** * generates a rectangle bar with the same size as the status bar * * @param activity requires Set activity * * @param color status bar color value * * @return status bar Rectangle Bar */private static View Createstatusview (Activity activit Y, int color) {//Get status bar height int resourceId = activity.getresources (). Getidentifier ("Status_bar_height", "dime        N "," Android ");        int statusbarheight = Activity.getresources (). Getdimensionpixelsize (ResourceId);        Draw a rectangle with the same height as the status bar view Statusview = new view (activity); Linearlayout.layoutparams params = new Linearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, Statu        Sbarheight);        Statusview.setlayoutparams (params);        Statusview.setbackgroundcolor (color);return statusview; /** * Set the status bar color * * @param activity required to set the activity * @param color status bar color value */@TargetApi (build.version_codes. Ice_cream_sandwich) public static void SetColor (activity activity, int color) {if (Build.VERSION.SDK_INT >= Build.version_codes. KITKAT) {//Set status bar Transparent Activity.getwindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATU            S);            Generate a status bar size rectangle View statusview = createstatusview (activity, color);            Add Statusview to layout viewgroup Decorview = (viewgroup) Activity.getwindow (). Getdecorview ();            Decorview.addview (Statusview); Set the parameters of the root layout viewgroup Rootview = (viewgroup) ((ViewGroup) Activity.findviewbyid (Android.            r.id.content)). Getchildat (0);            Rootview.setfitssystemwindows (TRUE);        Rootview.setcliptopadding (TRUE); }    }}

Step two: Add in the Oncreat () method in the Mainactivity class

Viewcolor.setcolor (This,getresources (). GetColor (r.color.main_red));//red background is the color that needs the status bar to change

Step three: Join the root node of the main layout file

  Android:fitssystemwindows= "true"

http://blog.csdn.net/codekxx/article/details/58079960

Android Development set immersive title 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.