Android Custom status bar and navigation bar analysis and implementation

Source: Internet
Author: User
<span id="Label3"></p>Effect<p><p>Android 4.4, the system is to support the custom status bar and navigation bar, The most typical example is the Bilibili client (ios version and Android version can use two sets of completely different system design ui, conscience ~), The top of the status bar is pink, The bottom navigation bar is a translucent color:<br>   <br>Then the latest version of QQ 6.2 also used the status bar transparent style, but out of the effect in different versions, different phones, the display of the effect is really very different (4.3 version is unable to use the status bar transparent style, just put out to do a contrast):<br>   <br>————————————————————————————————————<br>    <br>————————————————————————————————————<br>   <br>————————————————————————————————————<br>    <br>————————————————————————————————————<br>   <br>This I do not know exactly how to fit, I hope someone to answer.</p></p>Implementation and Analysis API 19~20<p><p>Next analyze how to customize the status bar and navigation bar, This before and after the 21 version can be implemented in different ways, first look at the 19~20 version of the adaptation, status bar and navigation bar transparent:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">resources</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">style</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"activity_translucent_status_bar"</span> <span class="hljs-attribute">Parent</span>=<span class="hljs-value">"@style/ Theme.AppCompat.Light.NoActionBar "</span>></span></span><span class="css"><span class="css"><<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: windowtranslucentstatus"</span>><span class="hljs-tag">true</span> </<span class="hljs-tag">Item</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">style</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">style</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"activity_translucent_navigation_bar"</span> <span class="hljs-attribute">Parent</span>=<span class="hljs-value">"@style/ Theme.AppCompat.Light.NoActionBar "</span>></span></span><span class="css"><span class="css"><<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: windowtranslucentstatus"</span>><span class="hljs-tag">true</span> </<span class="hljs-tag">Item</span>> <<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: Windowtranslucentnavigation "</span>><span class="hljs-tag">true</span></<span class="hljs-tag">item</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">style</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Resources</span>></span></span></code></pre><p><p>As seen from the code, the status bar transparency is primarily using the Android:windowtranslucentstatus property, and the navigation bar transparency is primarily using the Android:windowtranslucentnavigation Property. When this property is set to true, the system bar becomes translucent, and the content area of the app is expanded to the system bar:<br>   <br>This goal is achieved, but the effect is certainly not, how to solve it? To learn about the layout of Bilibili and QQ to Understand:<br>   <br>   <br>From the picture can be very clear see Bilibili and QQ are placed at the top of a and status bar, the height of the custom color View,status bar to obtain the way:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-keyword">int</span><span class="hljs-keyword">id</span> = getResources()<span class="hljs-variable">.getIdentifier</span>(<span class="hljs-string">"status_bar_height"</span><span class="hljs-string">"dimen"</span><span class="hljs-string">"android"</span>);<span class="hljs-keyword">int</span> height = getResources()<span class="hljs-variable">.getDimensionPixelOffset</span>(<span class="hljs-keyword">id</span>);</code></pre></pre><p><p>This way of thinking is very clear, all the activity inherits from a base class activity, the base class Activity's layout file carries on the similar processing, finally also can achieve and bilibili the client the same effect (spit slot Huawei P6 's status bar Black shadow really ugly):<br>   <br>There are also related libraries on GitHub that can do the same thing, but the same principle is True.</p></p>API 21~++<p><p>After version 21 and 21, the system added more options to provide user-modified colors:<br>   <br>So you can change the status bar to the desired color by using the Android:colorprimarydark property, android:navigationbarcolor the color of the navigation bar by changing the property:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs applescript"><<span class="hljs-property">item</span><span class="hljs-property">name</span>=<span class="hljs-string">"android:colorPrimaryDark"</span>>@color/bar_color</<span class="hljs-property">item</span>><<span class="hljs-property">item</span><span class="hljs-property">name</span>=<span class="hljs-string">"android:navigationBarColor"</span>>@color/half_black_transparent</<span class="hljs-property">item</span>></code></pre></pre><p><p>Final Effect:<br><br>It seems that the navigation bar is unable to use transparent color, then the navigation bar transparent scheme does not work, can only continue to use the 19 version of the Scheme:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">resources</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">style</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"activity_translucent_status_bar"</span> <span class="hljs-attribute">Parent</span>=<span class="hljs-value">"@style/ Theme.AppCompat.Light.NoActionBar "</span>></span></span><span class="css"><span class="css"><<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: colorprimarydark"</span>><span class="hljs-at_rule">@<span class="hljs-keyword">color/ bar_color</item></span> </span> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">style</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">style</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"activity_translucent_navigation_bar"</span> <span class="hljs-attribute">Parent</span>=<span class="hljs-value">"@style/ Theme.AppCompat.Light.NoActionBar "</span>></span></span><span class="css"><span class="css"><<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: windowtranslucentstatus"</span>><span class="hljs-tag">true</span> </<span class="hljs-tag">Item</span>> <<span class="hljs-tag">item</span> <span class="hljs-tag">name</span>= "<span class="hljs-tag">android</span><span class="hljs-pseudo">: Windowtranslucentnavigation "</span>><span class="hljs-tag">true</span></<span class="hljs-tag">item</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">style</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Resources</span>></span></span></code></pre><p><p>finally, It is also necessary to add a custom view at the top of the status bar height, the last different version, the different models to adapt the Effect:<br><br><br>Adaptation effect is also possible, if there are other better solutions, pointing out, Thank you ~</p></p>Source<p><p>Https://github.com/zhaozepeng/Android_framework</p></p> <p><p>Android Custom status bar and navigation bar analysis and implementation</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.