打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# – 在Android 7.0上不会调用OnCreateOptionsMenu

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

在更新之前,我使用的是Android 6.0,它没有任何问题.
如果我在6.0的另一部手机上测试它仍然有效(相同的代码).

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

我的MainActivity.cs的一部分,我设置了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.

OnCreate覆盖:

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>
来源:https://www.icode9.com/content-1-316001.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
NoActionBar主题下如何添加OptionsMenu
求教android实现短信图标右上角添加数字的问题。。跪求大神。。。
Android之解决androidx.appcompat.widget.Toolbar去掉左边距
Android:Theme.Material和Theme.AppCompat
让你的 APP换上Material Design
开发App抽屉功能,彻底掌握了吗?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服