如何在Android中创建选项菜单? [英] How to create a option menu in android?

查看:424
本文介绍了如何在Android中创建选项菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c#和Xamarin Studio在Android应用程序中创建一个简单的选项菜单.我该怎么办?

I want to create a simple option menu in Android application with c# and Xamarin Studio. How can I do it?

我还没有发现任何C#示例.有人可以简单说明如何创建选项菜单吗?

I haven't found any C# example of this. Can someone simply explain how to create a option menu, please?

推荐答案

定义菜单

创建菜单的一种方法是使用Xamarin.Android项目的Resources/menu/文件夹中的XML文件.

Defining the menu

One way to create a menu is using a XML file placed in the Resources/menu/ folder of your Xamarin.Android project.

例如:

资源/菜单/mymenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/item1"
        android:title="Item 1"/>
  <item android:id="@+id/item2"
        android:title="Item 2"/>
  <item android:id="@+id/item3"
        android:title="Item 3"/>
</menu>

要查看可以在菜单xml文件中定义的其他选项,请参见

To see what other options you can define in a menu xml file, please see the official documentation.

您可以在多个位置添加xml菜单. 一些例子:

You can inflate a xml menu in multiple locations. A few examples:

Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.mytoolbar);
toolbar.InflateMenu(Resource.Menu.mymenu);

处理点击

要处理工具栏菜单上的单击事件,您必须通过覆盖以下方法来实现Toolbar.IOnMenuItemClickListener界面:

To handle click events on a toolbar menu you have to implement the Toolbar.IOnMenuItemClickListener interface by overriding the following method:

public bool OnMenuItemClick(IMenuItem item)
{
    switch (item.ItemId)
    {
        case Resource.Id.item1: 
                //Do stuff for item1
                return true;
        case Resource.Id.item2: 
                //Do stuff for item2
                return true;
        case Resource.Id.item3:
                //Do stuff for item3
                return true;
        default:
                return false;
     }
}

然后,您必须将实现该接口的类添加为工具栏上的侦听器:

You then have to add the class implementing the interface to the toolbar as a listener:

toolbar.SetOnMenuItemClickListener(your_listener_class);

在活动"或片段"的默认菜单位置(已弃用)

在大多数情况下,活动或片段的默认菜单位置是硬件菜单按钮或ActionBar. 可以通过重写以下方法来在此处添加菜单:

In the default menu location of an Activity or Fragment (DEPRECATED)

In most cases the default menu location of an activity or fragment is either the hardware menu button or the ActionBar. Adding a menu here can be accomplished by the overriding the following method:

在活动中:

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

片段中:

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
    inflater.Inflate(Resource.Menu.mymenu, menu);
}

请确保您在Fragment的onCreate中将HasOptionsMenu设置为true,以使其正常工作.

Make sure you have HasOptionsMenu set to true in the onCreate of the Fragment for this to work.

处理点击

然后您可以通过覆盖OnOptionsItemSelected

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
       case Resource.Id.item1: 
                //Do stuff for item1
                return true;
        case Resource.Id.item2: 
                //Do stuff for item2
                return true;
        case Resource.Id.item3:
                //Do stuff for item3
                return true;
        default:
                return false;
     }
}

处理完选定的项目后,我们返回true来通知系统.

After we have handled the selected item we return true to notify the system of this.

一个非常基本的菜单是通过覆盖这样的OnCreateOptionsMenu方法来完成的:

A very basic menu is accomplished by overriding the OnCreateOptionsMenu method like this:

public override bool OnCreateOptionsMenu(IMenu menu)
{
      menu.Add(0,0,0,"Item 0");
      menu.Add(0,1,1,"Item 1");
      menu.Add(0,2,2,"Item 2");
      return true;
}

然后您可以通过覆盖OnOptionsItemSelected方法来处理选项菜单中的点击.

You can then handle clicks in the option menu by overriding the OnOptionsItemSelected method.

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
        case 0: 
                //Do stuff for item 0
                return true;
        case 1: 
                 //Do stuff for item 1
                return true;
        case 2: 
                //Do stuff for item 2
                return true;
        default:
                return false;
     }
}

这篇关于如何在Android中创建选项菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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