在 Android 7.0 上不会调用 OnCreateOptionsMenu [英] OnCreateOptionsMenu doesn't get called on Android 7.0

查看:31
本文介绍了在 Android 7.0 上不会调用 OnCreateOptionsMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题中提到的,在我将系统更新到 Android 7.0 后,OnCreateOptionsMenu 方法不会被调用.

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

在更新之前,我使用了 Android 6.0,它运行没有任何问题.如果我用 6.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).

这个方法在 Android 7.0 上有什么问题或者我的代码有问题吗?

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

我在 MainActivity.cs 中设置的部分 工具栏

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>

推荐答案

由于您使用的是 AppCompatActivity,您应该使用 Android.Support.V7.Widget.Toolbar 代替Android.Widget.Toobar 并调用 SetSupportActionBar 而不是 SetActionBar.现在您的 OnCreateOptionsMenu 将被调用.

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.

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 更新:

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

这篇关于在 Android 7.0 上不会调用 OnCreateOptionsMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆