Android Navigation Drawer, custom Actionbar (title centered)

Source: Internet
Author: User

The entire example is a navigationdrawer example that has been transformed from Google Android Training (http://developer.android.com/training/ implementing-navigation/nav-drawer.html)

Because I need to use a custom ActionBar, and Actionbardrawertoggle can only be triggered by the Action menu in ActionBar, and I need to provide a indicator picture, The triggering of the custom button is not supported (the following code):

 public   Boolean onoptionsitemselected ( MenuItem item) { if  (item! = null  &A mp;& Item.getitemid () = = Id_home && mdrawerindicatorenabled) { if   (Mdrawerlayout.isdrawervisible (Gravitycompat.start)) {mdrawerlayout.         Closedrawer (Gravitycompat.start);  else   {Mdrawerlayout.opendrawer (Gr         Avitycompat.start);   return  true  ;  return  false  ; }

So consider the reference actionbardrawertoggle rewrite your own mabdrawertoggle, and then remove the onoptionsitemselected way to trigger the drawer, a new Switchdrawer method, as follows:

 Public void Switchdrawer () {     if  (mdrawerlayout.isdrawervisible (Gravitycompat.start))     {         Mdrawerlayout.closedrawer (Gravitycompat.start);     }      Else     {         mdrawerlayout.opendrawer (gravitycompat.start);     }}

below to see the ActionBar custom View title centered problem, tossing for a long time, finally adopted framelayout way to solve, the following action_bar_title.xml:

<?xml version="1.0"encoding="Utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="wrap_content"> <TextView Android:id="@android: Id/title"Android:layout_height="match_parent"Android:layout_width="match_parent"android:gravity="Center"Android:text="Hello"Android:textcolor="@android: Color/holo_red_light"/> <ImageButton Android:id="@+id/left_btn"Android:layout_height="wrap_content"Android:layout_width="wrap_content"android:layout_gravity="left|center_vertical"Android:layout_marginleft="12.0dip"android:src="@android:d Rawable/ic_media_play"/> <ImageButton Android:id="@+id/right_btn"Android:layout_height="wrap_content"Android:layout_width="wrap_content"android:layout_gravity="right|center_vertical"Android:layout_marginright="12.0dip"android:src="@android:d rawable/ic_media_pause"/> </FrameLayout>

The following are implemented in OnCreate:

protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     Setcontentview (R.layout.activity_main); Mtitle=GetTitle (); Mplanettitles=getresources (). Getstringarray (R.array.planets_array); Mdrawerlayout=(drawerlayout) Findviewbyid (r.id.drawer_layout); Mdrawerlist=(ListView) Findviewbyid (r.id.left_drawer); //set a custom shadow that overlays the main content when the drawer opens Www.it165.netMdrawerlayout.setdrawershadow (R.drawable.drawer_shadow, Gravitycompat.start); Mdrawerlist.setadapter (NewArrayadapter<string> ( This, R.layout.drawer_list_item, mplanettitles)); Mdrawerlist.setonitemclicklistener (NewDraweritemclicklistener ()); Actionbar.layoutparams LP=Newactionbar.layoutparams (ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PAR     ENT, Gravity.center); View Viewtitlebar= Getlayoutinflater (). Inflate (R.layout.action_bar_title,NULL);     Getactionbar (). Setcustomview (Viewtitlebar, LP); Getactionbar (). setdisplayshowhomeenabled (false); Getactionbar (). setdisplayshowtitleenabled (false);     Getactionbar (). Setdisplayoptions (Actionbar.display_show_custom); Getactionbar (). setdisplayshowcustomenabled (true); Tvtitle=(TextView) Getactionbar (). Getcustomview (). Findviewbyid (Android.     R.id.title); ImageButton Ibtnnav=(ImageButton) Getactionbar (). Getcustomview (). Findviewbyid (R.ID.LEFT_BTN); Ibtnnav.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {mdrawertoggle.switchdrawer ();     }     }); //Mabdrawertoggle ties together the the proper interactions//between the sliding drawer and the Action Bar app iconMdrawertoggle =NewMabdrawertoggle ( This,/*host Activity*/Mdrawerlayout,/*Drawerlayout Object*/R.drawable.ic_drawer,/*nav drawer image to replace ' up ' caret*/R.string. Drawer_open,/*"Open drawer" description for accessibility*/R.string. drawer_close/*"Close drawer" description for accessibility*/            );     Mdrawerlayout.setdrawerlistener (Mdrawertoggle); if(Savedinstancestate = =NULL) {SelectItem (0); } }

Here are two runs:

After selecting an item in the Navigation list, toggle Fragment and Title:

Android Navigation Drawer, custom Actionbar (title centered)

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.