2016-09-14 122 views
2

我正在构建Xamarin Android应用程序,我想在自定义操作栏中实现带抽屉的右侧导航抽屉。一切正常(抽屉从右侧滑出,......),除了一件事情:导航抽屉图标未显示。未显示导航抽屉图标

所以,这是我的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> 

这是我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); 
    } 
    ... 
} 

这是我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> 

难道我做错了什么?任何帮助表示赞赏。

UPDATE

我CustomActionBar有应用程序图标和应用的标题。这可能会中断导航抽屉图标?

+0

是你能解决这个问题? – user427969

+0

@ user427969我在下面发布了我的解决方案,所以如果它解决了您的问题,请投票 – PeMaCN

回答

0

这是我已经找到了解决办法。

首先,你需要添加action_bar.xml文件包含以下内容的菜单文件夹:

<?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> 

然后在你的活动中插入此代码:

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); 
} 
0

不要忘记调用actionbarDrawerToggle.syncState()

+0

我在'OnPostCreate()'方法中调用它,如果我在'OnCreate()'方法中调用它,它也不会显示。 – PeMaCN

+0

我觉得默认的drawerToggle在左边。尝试把你的抽屉放在左边,检查图标是否出现。如果显示,我认为你应该在操作栏中创建自己的图标。 – Chenmin

+0

不,图标不显示 – PeMaCN