0

所以我看了这个youtube(https://www.youtube.com/watch?v=TahxgjZTpSA&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l&index=49)教程并使用了他的示例代码(示例50:http ://cforbeginners.com/XamarinProjects.html )在他的网站上找到。我运行了示例,出现了 Hamburger to Arrow 动画,但是当我尝试在我的解决方案项目中实现它时,没有动画或图标。

我什至去测试。我创建了一个新解决方案,并将示例文件复制到新解决方案中,结果相同,没有动画或图标。所以在这一点上,我认为是内部导致没有汉堡包到箭头动画或图标的问题。

我尝试了这个解决方案,但它们都不起作用:

ActionBarDrawerToggle.syncState 不显示汉堡图标?

Appcompatv7 - v21 导航抽屉不显示汉堡包图标

带有汉堡的导航抽屉到棒棒糖上的后退箭头动画

样本文件:

主要活动:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using SupportToolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using System.Collections.Generic;

namespace test.Droid
{
    [Activity (Label = "test.Droid", Icon = "@drawable/icon", MainLauncher = true, Theme="@style/MyTheme")]
    public class MainActivity : ActionBarActivity
    {
        private SupportToolbar mToolbar;
        private MyActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ListView mLeftDrawer;
        private ListView mRightDrawer;
        private ArrayAdapter mLeftAdapter;
        private ArrayAdapter mRightAdapter;
        private List<string> mLeftDataSet;
        private List<string> mRightDataSet;

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

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
            mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);
            mRightDrawer = FindViewById<ListView>(Resource.Id.right_drawer);

            mLeftDrawer.Tag = 0;
            mRightDrawer.Tag = 1;

            SetSupportActionBar(mToolbar);

            mLeftDataSet = new List<string>();
            mLeftDataSet.Add ("Left Item 1");
            mLeftDataSet.Add ("Left Item 2");
            mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataSet);
            mLeftDrawer.Adapter = mLeftAdapter;

            mRightDataSet = new List<string>();
            mRightDataSet.Add ("Right Item 1");
            mRightDataSet.Add ("Right Item 2");
            mRightAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mRightDataSet);
            mRightDrawer.Adapter = mRightAdapter;

            mDrawerToggle = new MyActionBarDrawerToggle(
                this,                           //Host Activity
                mDrawerLayout,                  //DrawerLayout
                Resource.String.openDrawer,     //Opened Message
                Resource.String.closeDrawer     //Closed Message
            );

            mDrawerLayout.SetDrawerListener(mDrawerToggle);
            SupportActionBar.SetHomeButtonEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(true);
            mDrawerToggle.SyncState();

            if (bundle != null)
            {
                if (bundle.GetString("DrawerState") == "Opened")
                {
                    SupportActionBar.SetTitle(Resource.String.openDrawer);
                }

                else
                {
                    SupportActionBar.SetTitle(Resource.String.closeDrawer);
                }
            }

            else
            {
                //This is the first the time the activity is ran
                SupportActionBar.SetTitle(Resource.String.closeDrawer);
            }
        }

        public override bool OnOptionsItemSelected (IMenuItem item)
        {       
            switch (item.ItemId)
            {

            case Android.Resource.Id.Home:
                //The hamburger icon was clicked which means the drawer toggle will handle the event
                //all we need to do is ensure the right drawer is closed so the don't overlap
                mDrawerLayout.CloseDrawer (mRightDrawer);
                mDrawerToggle.OnOptionsItemSelected(item);
                return true;

            case Resource.Id.action_refresh:
                //Refresh
                return true;

            case Resource.Id.action_help:
                if (mDrawerLayout.IsDrawerOpen(mRightDrawer))
                {
                    //Right Drawer is already open, close it
                    mDrawerLayout.CloseDrawer(mRightDrawer);
                }

                else
                {
                    //Right Drawer is closed, open it and just in case close left drawer
                    mDrawerLayout.OpenDrawer (mRightDrawer);
                    mDrawerLayout.CloseDrawer (mLeftDrawer);
                }

                return true;

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

        public override bool OnCreateOptionsMenu (IMenu menu)
        {
            MenuInflater.Inflate (Resource.Menu.action_menu, menu);
            return base.OnCreateOptionsMenu (menu);
        }

        protected override void OnSaveInstanceState (Bundle outState)
        {
            if (mDrawerLayout.IsDrawerOpen((int)GravityFlags.Left))
            {
                outState.PutString("DrawerState", "Opened");
            }

            else
            {
                outState.PutString("DrawerState", "Closed");
            }

            base.OnSaveInstanceState (outState);
        }

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

        public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            mDrawerToggle.OnConfigurationChanged(newConfig);
        }
    }
}

MyActionBarDrawerToggle:

using System;
using SupportActionBarDrawerToggle = Android.Support.V7.App.ActionBarDrawerToggle;
using Android.Support.V7.App;
using Android.Support.V4.Widget;

namespace test.Droid
{
    public class MyActionBarDrawerToggle : SupportActionBarDrawerToggle
    {
        private ActionBarActivity mHostActivity;
        private int mOpenedResource;
        private int mClosedResource;

        public MyActionBarDrawerToggle (ActionBarActivity host, DrawerLayout drawerLayout, int openedResource, int closedResource) 
            : base(host, drawerLayout, openedResource, closedResource)
        {
            mHostActivity = host;
            mOpenedResource = openedResource;
            mClosedResource = closedResource;
        }

        public override void OnDrawerOpened (Android.Views.View drawerView)
        {   
            int drawerType = (int)drawerView.Tag;

            if (drawerType == 0)
            {
                base.OnDrawerOpened (drawerView);
                mHostActivity.SupportActionBar.SetTitle(mOpenedResource);
            }
        }

        public override void OnDrawerClosed (Android.Views.View drawerView)
        {
            int drawerType = (int)drawerView.Tag;

            if (drawerType == 0)
            {
                base.OnDrawerClosed (drawerView);
                mHostActivity.SupportActionBar.SetTitle(mClosedResource);
            }               
        }

        public override void OnDrawerSlide (Android.Views.View drawerView, float slideOffset)
        {
            int drawerType = (int)drawerView.Tag;

            if (drawerType == 0)
            {
                base.OnDrawerSlide (drawerView, slideOffset);
            }
        }
    }
}

款式:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>

    <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#2196F3</item>
        <item name="drawerArrowStyle">@style/MyDrawerArrowStyle</item>
    </style>

    <style name="MyDrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="color">#F5F5F5</item>
        <item name="spinBars">true</item>
    </style>
</resources>

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!-- The Main Content View -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Support V7 Action Bar Drawer Toggle"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    <!-- The Left Navigation Drawer -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#818181"
            android:dividerHeight="1dp"
            android:background="#E3F2FD" />
        <ListView
            android:id="@+id/right_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:choiceMode="singleChoice"
            android:divider="#E2E2E2"
            android:dividerHeight="1dp"
            android:background="#9E9E9E" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

菜单:

<?xml version="1.0" encoding="UTF-8" ?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myapp="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:title="Refresh"
        myapp:showAsAction="always" />

    <item android:id="@+id/action_help"
        android:icon="@drawable/ic_action_help"
        android:title="Help"
        myapp:showAsAction="always"/>

</menu>
4

1 回答 1

1

您错过了对 的SetDisplayHomeAsUpEnabled()调用ActionBar

SupportActionBar.SetDisplayHomeAsUpEnabled(true);
于 2016-01-29T22:55:00.027 回答