Android, How to create option Menu

后端 未结 11 1824
执笔经年
执笔经年 2020-11-27 14:11

Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...

MenuTest.java

public class Men         


        
相关标签:
11条回答
  • 2020-11-27 15:05
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.*;
    import android.widget.*;
    
    public class AndroidWalkthroughApp2 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // show menu when menu button is pressed
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.options_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // display a message when a button was pressed
            String message = "";
            if (item.getItemId() == R.id.option1) {
                message = "You selected option 1!";
            }
            else if (item.getItemId() == R.id.option2) {
                message = "You selected option 2!";
            }
            else {
                message = "Why would you select that!?";
            }
    
            // show message via toast
            Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
            toast.show();
    
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:10
    public class MenuTest extends Activity {
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.more_tab_menu, menu);
    
            // return true so that the menu pop up is opened
            return true; 
        }
    }
    

    and don't forget to press the menu button or icon on Emulator or device

    0 讨论(0)
  • 2020-11-27 15:12

    please see :==

    private int group1Id = 1;
    
    int homeId = Menu.FIRST;
    int profileId = Menu.FIRST +1;
    int searchId = Menu.FIRST +2;
    int dealsId = Menu.FIRST +3;
    int helpId = Menu.FIRST +4;
    int contactusId = Menu.FIRST +5;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);
        menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);
        menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);
        menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);
        menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);
        menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);
    
        return super.onCreateOptionsMenu(menu); 
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            // write your code here
            Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);
            msg.show();
            return true;
    
        case 2:
            // write your code here
            return true;
    
        case 3:
            // write your code here
            return true;
    
        case 4:
            // write your code here
            return true;
    
        case 5:
            // write your code here
            return true;
    
        case 6:
            // write your code here
            return true;
    
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:13

    The previous answers have covered the traditional menu used in android. Their is another option you can use if you are looking for an alternative

    https://github.com/AnshulBansal/Android-Pulley-Menu

    Pulley menu is an alternate to the traditional Menu which allows user to select any option for an activity intuitively. The menu is revealed by dragging the screen downwards and in that gesture user can also select any of the options.

    0 讨论(0)
  • 2020-11-27 15:13

    Good Day I was checked And if You choose Empty Activity You Don't have build in Menu functions For Build in You must choose Basic Activity In this way You Activity will run onCreateOptionsMenu

    Or if You work in Empty Activity from start Chenge in styles.xml the

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