OnCreateOptionsMenu doesn't get called on Android 7.0

纵饮孤独 提交于 2019-12-10 16:27:56

问题


As in the title mentioned the OnCreateOptionsMenu method doesn't get called after I updated my system to Android 7.0.

Before the update I used Android 6.0 and it worked without any issues. If I test it on my other phone with 6.0 it is still working (same code).

Is there any problem with this method on Android 7.0 or is something wrong in my code?

The part of my MainActivity.cs where I set the toolbar

[Activity(Label = "App", Icon = "@drawable/icon", MainLauncher = true, Theme = "@style/Theme.AppCompat.Light.NoActionBar",ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
         base.OnCreate(bundle);
         SetContentView(Resource.Layout.Main);

         var toolbar = FindViewById<Android.Widget.Toolbar>(Resource.Id.toolbar);
         toolbar.SetTitleTextColor(Color.White);
         SetActionBar(toolbar);
    }

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

Main.axml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1D1D1D"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />
</RelativeLayout>

top_menu_start

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
       android:id="@+id/start_listview"
       android:icon="@drawable/icon_posts_list"
       android:showAsAction="ifRoom"
       android:title="startListview" />
  <item
       android:id="@+id/start_pager"
       android:icon="@drawable/icon_posts_kacheln"
       android:showAsAction="ifRoom"
       android:title="startPager" />
  <item
       android:id="@+id/doSomething"
       android:icon="@drawable/icon"
       android:showAsAction="ifRoom"
       android:title="doSomething" />
</menu>

回答1:


Since you are using AppCompatActivity you should be using Android.Support.V7.Widget.Toolbar instead of Android.Widget.Toobar and calling SetSupportActionBar instead of SetActionBar. Now your OnCreateOptionsMenu will be called.

OnCreate override:

base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.SetTitleTextColor(Color.White);
SetSupportActionBar(toolbar);

Main.axml update:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:background="#1D1D1D"        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar">
    </android.support.v7.widget.Toolbar>


来源:https://stackoverflow.com/questions/43721141/oncreateoptionsmenu-doesnt-get-called-on-android-7-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!