Android Status Bar Summary

Source: Internet
Author: User
<span id="Label3"></p> <blockquote> <blockquote> <ol> <li>For the operation of the status bar, only for 4.4kitKat (including) more than the model, some of the domestic ROM will expire, the current discovery of Huawei's Emui</li> <li>Activity must be a <code>noActionbar</code> subject</li> <li>This article is based on statusbarutils slightly modified, thanks to the author Laobie</li> <li>The source address of this article</li> </ol> </blockquote> </blockquote>Related Properties Revisited <ul> <ul> <li>Fitssystemwindows<br>When using <code>FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS</code> and <code>FLAG_TRANSLUCENT_STATUS</code> crowding out the height of the <strong>status bar</strong> , our layout files are then topped to the status bar. Through <code>FitsSystemWindows</code> , the system will change the app layout file to the height of the <code>paddingTop</code> status bar, to achieve the appropriate effect<br><br>False top to status bar</li> </ul> </ul><br>True,paddingtop has a status bar height <ul> <ul> <li><p>Android. R.id.content<br>Through it we can access the <strong>current Activity</strong> root node without needing to know the id, generally the <code>ContentFrameLayout</code> type, through the <code>((ViewGroup)findViewById(R.id.content)).getChildAt(0);</code> root node that we can get to our <strong>layout xml</strong></p></li> <li><p>Getdecorview ()<br>Through him, we can get to the top view of the entire window Interface. Much of what we use <code>findViewById()</code> is based on the <code>DecorView</code> child nodes in the Search. For example, the activity theme is <code>Theme.AppCompat.Light.NoActionBar</code> the following organizational hierarchy,</p></li> </ul> </ul><br>Noactionbarview hierarchy. png<p><p><em>Why nesting so deep, because each level may have a few <code>ViewStub</code> convenient injection, but our current theme is not available is Empty.</em></p></p> <blockquote> <blockquote> <p>The following starts the actual combat, generally we use more is transparent status bar, set the status bar color, <code>DrawerLayout</code> cover in the status bar three KINDS.</p> </blockquote> </blockquote>1. Transparent status bar<p><p>This is typically used to extend the picture to the status bar, and the picture is divided into two</p></p> <ol> <ol> <li>As <code>background</code> a full-screen large image</li> <li>exists as a <code>ImageView</code> control, typically only part of the screen height</li> </ol> </ol><p><p>In both cases, the approach is different, see below</p></p><pre class="hljs less"><code class="less"><span class="hljs-comment">/** * Make the status bar transparent */<span class="hljs-selector-tag">Private<span class="hljs-selector-tag">Static <span class="hljs-selector-tag"><span class="hljs-selector-tag">void <span class="hljs-selector-tag">transparentstatusbar (activity Activity) { Span class= "hljs-selector-tag" >if (Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {<span class="hljs-selector-tag">activity<span class="hljs-selector-class">.getwindow () <span class=" Hljs-selector-class ">.addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); <span class="hljs-selector-tag">activity<span class="hljs-selector-class">.getwindow () <span class="hljs-selector-class">.setstatusbarcolor (color.transparent);} <span class="hljs-selector-tag">else {<span class="hljs-selector-tag">activity <span class="hljs-selector-class">.getwindow () <span class="hljs-selector-class">.addflags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p>The above code for more than 5.0 or more than 4.4 5.0 or less of the model, so that its status bar transparent, the navigation bar translucent, the effect is as Follows:</p></p> <br>The result of the transparent status bar is that the layout file is top to the status bar<p><p><br>Dump the next node information, we found that the layout of the paddingtop from (50px status bar Height) was modified to 0px. So that the picture extends to the status bar</p></p><br>Opaque status bar. png<br>Transparent status bar. PNG<p><p><br>In this case, the app layout file <code>FitsSystemWindows</code> can be set to complete the appropriate</p></p><pre class="hljs cpp"><code class="cpp"><span class="hljs-comment"><span class="hljs-comment">/** * Get activity root node * @param activity * @return */<span class="hljs-function"><span class=" Hljs-keyword ">public <span class=" hljs-keyword ">static viewgroup <span class=" hljs-title ">getAppRootView<span class="hljs-keyword" (activity activity) {<span>return (viewgroup) (viewgroup) Activity.findviewbyid (android. r.id.content). getchildat (<span class="hljs-number">0);} <span class="hljs-comment">/** * Set the paddingtop of the target view from 0 to the height of the status bar */<span class="hljs-function"><span class="hljs-keyword"> Private <span class="hljs-keyword">static <span class="hljs-keyword">void <span class="hljs-title"> Setrootviewfitsystemwindow<span class="hljs-literal" (activity activity) {viewgroup rootview="Getapprootview" ( activity); rootview.setfitssystemwindows (<span>true); rootview.setcliptopadding ( <span class="hljs-literal">true), </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></code></pre> <br>The app content layout is Padding.png<p><p><br><strong>attention!! </strong>to extend the picture to the status bar, if it is not the background of the root node, but rather a <strong>ImageView</strong> control, then you cannot set <code>setFitsSystemWindows</code> the reason reference to the red box. In this case, in order to prevent the top to the status bar, you need to manually in the XML through <code>paddingTop</code> , <code>marginTop</code> adjust the title bar element position to achieve Adaptation. Similar</p></p><br>The picture that extends to the status bar is a ImageView2. Status bar Color<p><p>The steps to set the status bar color are similar to transparent, except that the 5.0 model cannot directly set the status bar color, and we can simulate a solid color view and put it in the status bar to indirectly achieve the shading effect. The parent level of placement we choose<code>DecorView</code></p></p> <blockquote> <blockquote> <p>If the status bar color setting is found to be invalid, it is possible that the root node of the layout file was added <code>backgroundColor</code></p> </blockquote> </blockquote><pre class="hljs aspectj"><code class="aspectj"><span class="hljs-comment">/** * Set the status bar color * *<span class="hljs-doctag">Activity to be set @param activity *<span class="hljs-doctag">@param color status Bar value *<span class="hljs-doctag">@param statusbaralpha status bar Transparency */<span class="hljs-keyword">Public<span class="hljs-keyword">Static<span class="hljs-function"><span class="hljs-function"><span class="hljs-keyword">void <span class="hljs-title">setcolor <span class="hljs-keyword" (activity activity, <span>int color, <span class="hljs-keyword">int Statusbaralpha) {<span class="hljs-keyword">if (Build.VERSION.SDK_INT < Build.version_codes. KITKAT) <span class="hljs-keyword">return; <span class="hljs-keyword">if (Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {activity.getwindow (). addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); Activity.getwindow (). setstatusbarcolor (uiutils.calccolorwithalpha (color, statusbaralpha)); } <span class="hljs-keyword">else {activity.getwindow (). addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_ STATUS); ViewGroup Decorview = (viewgroup) Activity.getwindow (). getdecorview (); Insertmockstatusbackgroundview (decorview, color, statusbaralpha); } Setrootviewfitsystemwindow (activity);} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></code></pre> <blockquote> <blockquote> <p>The above code determines whether the No. 0 view under Decorview is our custom Statusbarview type, or if it is set to a color, otherwise creates a solid color control to insert <code>DecorView</code> inside, while the layout content is vacated to a solid color control.</p> </blockquote> </blockquote>3. Drawerlayout cover on the status bar <blockquote> <blockquote> <p>Drawerlayout There are two main sub-elements 1: content view 2: Slide bar<br>The slide bar must be behind the content view to overwrite the former</p> </blockquote> </blockquote><br>Slide Bar Effect<p><p><br>So we have 2 steps to do.</p></p> <ol> <ol> <li>Insert a Solid-color control into the content view (if not linarlayout need to consider the content view element rearranged)</li> <li>Drawerlayout settings <code>fitSystemWindow=false</code> to overwrite on the status bar</li> </ol> </ol><p><p>The code is as Follows:</p></p><pre class="hljs aspectj"><code class="aspectj"><span class="hljs-comment">/** * Set status bar for drawerlayout layout color * *<span class="hljs-doctag">Activity to be set @param activity *<span class="hljs-doctag">@param drawerlayout Drawerlayout *<span class="hljs-doctag">@param color status Bar value *<span class="hljs-doctag">@param statusbaralpha status bar Transparency */<span class="hljs-keyword">Public<span class="hljs-keyword">Static<span class="hljs-function"><span class="hljs-keyword">void<span class="hljs-title">Setcolorfordrawerlayout<span class="hljs-params">(activity activity, drawerlayout drawerlayout,<span class="hljs-keyword">int color,<span class="hljs-keyword"><span class="hljs-keyword">int Statusbaralpha) { <span class="hljs-keyword">If (Build.VERSION.SDK_INT < Build.version_codes. KITKAT) { <span class="hljs-keyword">return;} <span class="hljs-keyword">if (Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {activity.getwindow (). addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); Activity.getwindow (). clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Activity.getwindow (). Setstatusbarcolor (color.transparent); } <span class="hljs-keyword">Else {activity.getwindow (). addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);} ViewGroup contentlayout = (viewgroup) Drawerlayout.getchildat (<span class="hljs-number">0); <span class="hljs-comment">//if The content view is not linearlayout, you need to rearrange the contents of the content view child element Insertmockstatusbackgroundview (contentlayout, color, statusbaralpha); Drawerlayout.setfitssystemwindows (<span class="hljs-keyword">false);}</span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>Summarize:<p><p>The above three methods cover the state of the app in most immersive scenarios. After understanding the underlying implementation, we can easily achieve iOS immersive effect on android, the minimum version 4.4 KitKat is now very popular, with miui, FlyMe successively upgraded to 5.0, more than 6.0, the charm of blue, Red rice and so on a large number of domestic low-end models can experience this cool effect.</p></p><p><p>Android Status Bar Summary</p></p></span>

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.