Android菜单项的ActionView属性始终为null

时间:2017-11-04 07:53:24

标签: xamarin menu actionview

我希望通过某种逻辑对Animate Menu项进行动画处理,但是id为refresh的菜单项在ActionView的属性中为null。我错了什么?

菜单项Xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto">
      <item
           android:id="@+id/refresh"
           android:icon="@drawable/plus_white"
           local:showAsAction="ifRoom"
           local:actionViewClass="android.support.v7.widget.ImageView"
           android:title="Refresh" />
      <item
           android:id="@+id/favoritePodcast"
           android:icon="@drawable/favorite_star"
           local:showAsAction="ifRoom"
           android:title="Favorite" />

    </menu>

OnCreateOption ::

     public override bool OnCreateOptionsMenu(IMenu menu)
            {
                MenuInflater.Inflate(Resource.Menu.MainMenu, menu);

                if (_notified)
                {
                    IMenuItem item = menu.FindItem(Resource.Id.refresh);
                    var itemView= item.ActionView.JavaCast<Android.Widget.ImageView>();

                    Animation anim = AnimationUtils.LoadAnimation(ApplicationContext,
                                                       Resource.Animation.fade_in);

                    itemView.StartAnimation(anim);
                    Toast.MakeText(this, "Notified", ToastLength.Short).Show();
                }

                return base.OnCreateOptionsMenu(menu);
            }

BreakPoint视图:

1 个答案:

答案 0 :(得分:0)

我找到了答案。 感谢robbit的帮助:https://forums.xamarin.com/profile/355640/robbit 我为菜单项定义了一个新的布局,如下所示:

NewItemRefresh.axml产品:&gt;&GT;&GT;&GT;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:layout_gravity="right" >

  <ImageView
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:clickable="true"
      android:src="@drawable/plus_white"/>

 </RelativeLayout>

并添加android:actionLayout =&#34; @ layout / NewItemRefresh&#34;到菜单项

现在菜单项已更改:

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto">
      <item
           android:id="@+id/refresh"
           android:icon="@drawable/plus_white"
           local:showAsAction="ifRoom"
           local:actionViewClass="android.support.v7.widget.ImageView" 
           android:actionLayout="@layout/NewItemRefresh"
           android:title="Refresh" />
      <item
           android:id="@+id/favoritePodcast"
           android:icon="@drawable/favorite_star"
           local:showAsAction="ifRoom"
           android:title="Favorite" />

    </menu> 

OnCreateOptionsMenu:

     public override bool OnCreateOptionsMenu(IMenu menu)
            {
                MenuInflater.Inflate(Resource.Menu.MainMenu, menu);

                if (_notified)
                {
                    IMenuItem item = menu.FindItem(Resource.Id.refresh);
                    View view = LayoutInflater.Inflate(Resource.Layout.NewItemRefresh, null);
                    item.SetActionView(view);
                    var itemView= item.ActionView.JavaCast<Android.Widget.RelativeLayout>();

                    Animation anim = AnimationUtils.LoadAnimation(ApplicationContext,
                                                       Resource.Animation.fade_in);

                    itemView.StartAnimation(anim);
                    Toast.MakeText(this, "Notified", ToastLength.Short).Show();
                }

                return base.OnCreateOptionsMenu(menu);
            }

所以我可以用一些逻辑来动画菜单项。