DrawerArrowToggle按钮没有显示在我的Android应用程序中?

时间:2015-09-15 09:09:11

标签: c# android xamarin appcompat-v7-r22.1

我正在开发一个带有导航抽屉的android项目。我正在研究xamarin android。当我使用 Android支持库V7 AppCompact (版本 22.2.0.0 )时,切换按钮工作正常,但是当我升级到最新版本时,即 22.2.1.0 < / strong>它没有显示。这是我的参考代码(与许多教程中提供的许多示例相同)

  

Styles.xml

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

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

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

Action_menu.xml

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

Main.axml

<?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"
            android:id="@+id/me">
            <RelativeLayout
                android:id="@+id/newsContainer"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone"
                android:antialias="true" />
            <LinearLayout
                android:id="@+id/newsList"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone" />
        </RelativeLayout>
    <!-- The Left Navigation Drawer -->
        <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="270dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#727272"
            android:orientation="vertical">
            <ImageView
                android:src="@drawable/news"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:id="@+id/image" />
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp">
                <ImageView
                    android:src="@drawable/settings"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView1" />
                <TextView
                    android:text="Settings"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8dp"
                    android:textColor="#fff8f8f8" />
            </LinearLayout>
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#B6B6B6" />
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp">
                <ImageView
                    android:src="@drawable/help"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView1" />
                <TextView
                    android:text="Help"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8dp"
                    android:textColor="#fff8f8f8" />
            </LinearLayout>
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#B6B6B6" />
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp">
                <ImageView
                    android:src="@drawable/info"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView1" />
                <TextView
                    android:text="About"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8dp"
                    android:textColor="#fff8f8f8" />
            </LinearLayout>
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#B6B6B6" />
        </LinearLayout>
    <!--The Right Navigation Drawer-->
        <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>

我需要做什么修改或其他任何事情来取回切换按钮,或者我必须参考以前版本的库?谢谢。任何帮助将不胜感激。 :)

  

onCreate(Bundle savedInstanceState)

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

            // 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<LinearLayout>(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 (savedInstanceState != null)
            {
                if (savedInstanceState.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);
            }
            progressDialog = new ProgressDialog (this);
            progressDialog.SetMessage("Loading...");
            // this.QueueListView(this.lista);
            GetFeedItemsList ();
           // SetContentView(Resource.Layout.NewsTileListItem);

        }

  

myActionBarDrawerToggle.cs

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

0 个答案:

没有答案