Android-drawerlayout Introduction

Source: Internet
Author: User
<span id="Label3"></p> <p><p>Drawerlayout has been out for a long time, personally think that the domestic app is deeply poisoned by iOS in the design are scrambling to imitate the style of ios, have forgotten what is the unique Android Style. I have to learn it first. then with the product to use a series of Android style in the project, it happened that drawerlayout menu navigation was won by me, used on the Project.</p></p><p><p>First drawerlayout exists in the V4 Package. We just need to configure it in XML to</p></p><pre class="prettyprint"><code class=" hljs avrasm"><android<span class="hljs-preprocessor"><span class="hljs-preprocessor">.</span> support</span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. V</span></span>4<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Widgets</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Drawerlayout</span></span>Android:id=<span class="hljs-string"><span class="hljs-string">"@+id/drawerlayout"</span></span>Xmlns:android=<span class="hljs-string"><span class="hljs-string">"http://schemas.android.com/apk/res/android"</span></span>Android:layout_width=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>android:layout_height=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>> <textview android:layout_width=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>android:layout_height=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>android:text=<span class="hljs-string"><span class="hljs-string">"@string/hello_world"</span></span>Android:textcolor=<span class="hljs-string"><span class="hljs-string">"@android: color/black"</span></span>android:gravity=<span class="hljs-string"><span class="hljs-string">"center"</span></span>/> <linearlayout android:layout_width=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>android:layout_height=<span class="hljs-string"><span class="hljs-string">"match_parent"</span></span>android:layout_gravity=<span class="hljs-string"><span class="hljs-string">"start"</span></span>Android:background=<span class="hljs-string"><span class="hljs-string">"#ffffff"</span></span>android:orientation=<span class="hljs-string"><span class="hljs-string">"vertical"</span></span>/></android<span class="hljs-preprocessor"><span class="hljs-preprocessor">.</span> support</span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. V</span></span>4<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Widgets</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Drawerlayout</span></span>></code></pre><p><p>In this need to introduce the next Actionbardrawertoggle. Controls the Display/hide use of Drawerlayout.<br>Use it in the two callback functions in the Activity:<br>Onconfigurationchanged<br>onoptionsitemselected<br>Call Actionbardrawertoggle.syncstate () in the Activity's onpostcreate (). Instructions. The Actionbardrawertoggle is synchronized with the Drawerlayout State. and set the drawer icon in the Actionbardrawertoggle to Actionbar's Home-button</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">draweractivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">appcompatactivity</span> {</span></span>Drawerlayout mdrawerlayout; Actionbardrawertoggle toggle;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.test_activity); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawerlayout); Toggle =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Actionbardrawertoggle (draweractivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, mdrawerlayout, r.string.hello_world, r.string.hello_world); Mdrawerlayout.setdrawerlistener (toggle); Getsupportactionbar (). sethomebuttonenabled (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>); Getsupportactionbar (). setdisplayhomeasupenabled (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>); Getsupportactionbar (). Settitle (<span class="hljs-string"><span class="hljs-string">"testdrawerlayout"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onpostcreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onpostcreate (savedinstancestate); Toggle.syncstate (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onconfigurationchanged</span></span>(Configuration Newconfig) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onconfigurationchanged (newconfig); Toggle.onconfigurationchanged (newconfig); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">onoptionsitemselected</span></span>(MenuItem Item) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(toggle.onoptionsitemselected (item))<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onoptionsitemselected (item); }}</code></pre><p><p>The above steps will allow you to successfully use Drawerlayout in your Project. But there are several points to note:<br>1.android.support.v4.widget.drawerlayout is present under the V4 package<br>2. Themes need to show actionbar, such as Theme.AppCompat.Light.DarkActionBar<br>3.ActionBarDrawerToggle is a drawerlistener implementation, we are able to write our own drawerlistener to achieve specific requirements.</p></p><p><p>We are able to find the unique Android style that we use is also very easy. And we are so much affected by ios.</p></p> <p><p>Android-drawerlayout Introduction</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.