c# – 在Android 7.0上不会调用OnCreateOptionsMenu
发布时间:2020-12-15 21:06:10 所属栏目:百科 来源:网络整理
导读:正如在上面提到的标题中那样,在我将系统更新到 Android 7.0之后,不会调用OnCreateOptionsMenu方法. 在更新之前,我使用的是Android 6.0,它没有任何问题. 如果我在6.0的另一部手机上测试它仍然有效(相同的代码). 这个方法在Android 7.0上有什么问题,或者我的代
正如在上面提到的标题中那样,在我将系统更新到
Android 7.0之后,不会调用OnCreateOptionsMenu方法.
在更新之前,我使用的是Android 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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |