How to create a option menu in android?

前端 未结 1 907
滥情空心
滥情空心 2021-02-13 22:10

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

I haven\'t found any C# example of this. Can someone simply exp

1条回答
  •  旧巷少年郎
    2021-02-13 22:52

    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.

    For example:

    Resources/Menu/mymenu.xml

    
      
      
      
    
    

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

    Using the menu

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

    In a Toolbar

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

    Handle clicks

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

    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:

    In a Activity:

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

    In a Fragment:

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

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

    Handle clicks

    You can then handle clicks to the menu by overriding 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;
         }
    }
    

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

    Alternative: Creating a menu programatically

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

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

    0 讨论(0)
提交回复
热议问题