0

そこで、この youtube ( https://www.youtube.com/watch?v=TahxgjZTpSA&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l&index=49 ) チュートリアルを見て、彼のサンプル コードを使用しました (サンプル 50: http://cforbeginners.com/XamarinProjects.html ) 。彼のウェブサイトで見つけました。サンプルを実行すると、ハンバーガーから矢印へのアニメーションが表示されましたが、ソリューション プロジェクトに実装しようとすると、アニメーションやアイコンが表示されませんでした。

私はテストのために遠くまで行きました。私は新しいソリューションを作成し、サンプル ファイルを新しいソリューションに文字通りコピーし、同じ結果をアニメーションやアイコンなしでコピーしました。この時点で、ハンバーガーから矢印へのアニメーションやアイコンがないという問題を内部的に引き起こしていると考えています。

私はこの解決策を試しましたが、どれも機能しません:

ActionBarDrawerToggle.syncState ハンバーガー アイコンが表示されませんか?

Appcompatv7 - v21 ナビゲーション ドロワーにハンバーガー アイコンが表示されない

Lollipop でバーガーから戻る矢印のアニメーションを含むナビゲーション ドロワー

サンプルのファイル:

主な活動:

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 に答える