Implementing an option menu in Android Studio

后端 未结 4 1657
名媛妹妹
名媛妹妹 2021-02-14 02:02

How do I implement an option menu in my android application? I tried code from Android Developer but I get errors. Such as these: Element menu must be declared.

4条回答
  •  滥情空心
    2021-02-14 02:31

    You should use onCreateOptionsMenu (Menu menu)

    Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu.

    This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).

    onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource.

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.option_menu, menu); // set your file name
            return super.onCreateOptionsMenu(menu);
        }
    

    Your option_menu.xml

        
    
        
        
        
    
     
    

    Please check demo Android Option Menu Example

提交回复
热议问题