简体   繁体   中英

Navigation drawer icon not showing

I'm building Xamarin Android application and I want to implement right navigation drawer with drawer toogle in a custom action bar. Everything is working (drawer slides from right side,...) except one thing: the navigation drawer icon is not showing.

So, this is my Activity.axml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <android.support.v4.widget.DrawerLayout
           android:orientation="vertical"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#ffffff"
           android:id="@+id/drawerLayout">

           <!-- Main Content -->
           <FrameLayout xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
               <fragment
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical"
                    android:id="@+id/map" />
                     ...
           </FrameLayout>

           <!-- Right Navigation Drawer -->
           <LinearLayout
               android:orientation="vertical"
               android:layout_width="250dp"
               android:layout_height="match_parent"
               android:layout_gravity="right"
               android:background="#f2f2f2">
               ...
            </LinearLayout>
     </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

And this is my Activity.cs :

using Android.Support.V4.App;
using Android.Support.V4.Widget;
... 

public class Activity : Activity
{
    ...
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToogle;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        ActionBar.SetDisplayShowHomeEnabled(false);
        ActionBar.SetDisplayShowTitleEnabled(false);
        ActionBar.SetCustomView(Resource.Layout.CustomActionBar);
        ActionBar.SetDisplayShowCustomEnabled(true);

        SetContentView(Resource.Layout.Activity2);

        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
        mDrawerToogle = new ActionBarDrawerToggle(this, mDrawerLayout, Resource.Drawable.ic_drawer,Resource.String.open_drawer_layout, Resource.String.close_drawer_layout);

        mDrawerLayout.SetDrawerListener(mDrawerToogle);
        ActionBar.SetDisplayHomeAsUpEnabled(true);
        ActionBar.SetHomeButtonEnabled(true);

        ...
    }

    protected override void OnPostCreate(Bundle savedInstanceState)
    {
        base.OnPostCreate(savedInstanceState);
        mDrawerToogle.SyncState();
    }

    public override void OnConfigurationChanged(Configuration newConfig)
    {
        base.OnConfigurationChanged(newConfig);
        mDrawerToogle.OnConfigurationChanged(newConfig);
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        if (mDrawerToogle.OnOptionsItemSelected(item))
        {
            return true;
        }

        return base.OnOptionsItemSelected(item);
    }
    ...
}

This is my Themes.xml :

<resources>
   <style name="CustomActionBarTheme"
     parent="@android:style/Theme.Holo.Light.DarkActionBar">
       <item name="android:actionBarStyle">@style/ActionBar</item>
   </style>
   <style name="ActionBar"
     parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
       <item name="android:height">75dp</item>
       <item name="android:background">#00000000</item>
   </style>
</resources>

Am I doing something wrong? Any help is appreciated.

UPDATE

My CustomActionBar has app icon and app title. Is this maybe interrupting navigation drawer icon?

不要忘记调用

This is the solution I have found.

First, you need to add action_bar.xml file with the following content to the menu folder:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:id="@+id/personalInfoActionBar"
    android:title="Informations"
    android:icon="@drawable/infoIcon"
    android:showAsAction="always"/>
</menu>

Then insert this code in your activity:

public override bool OnOptionsItemSelected(IMenuItem item)
{
    if (item.ItemId == Resource.Id.personalInfoActionBar)
    {
         if (mDrawerLayout.IsDrawerOpen(mRightDrawer))
         {
              mDrawerLayout.CloseDrawer(mRightDrawer);
         }
         else
         {
              mDrawerLayout.OpenDrawer(mRightDrawer);
         }

         return true;
    }

    return base.OnOptionsItemSelected(item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM