Android uses XML to define the Menu

Source: Internet
Author: User

Android uses XML to define the Menu


Adding menu items directly to the code and grouping menu items. This is a traditional practice and it has some shortcomings.
For example, to respond to each menu item, we need to use constants to save the ID of each menu item.
Therefore, Android provides a better way to define a menu as a resource of an application, and use android to locally support resources, this allows us to easily create and respond to menus. B
This article describes how to use an XML file to load and respond to menus. We need to perform these steps:


1. Create a menu folder under the/res directory
2. Use the menu-related elements in the menu directory to define the xml file. The file name is random and android will automatically generate a resource ID for it.
For example, R. menu. menu corresponds to the menu. xml resource file in the menu directory.
3. Use the resource ID of the xml file to add the menu items defined in the xml file to the menu object.
4. when responding to a menu item, use the resource ID corresponding to each menu item.


Package cn. fansunion. menu; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuInflater; import android. view. menuItem; import android. widget. toast; import cn. fansunion. r; public class OptionMenuWithXmlActivity extends Activity {public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. option_menu);} public boolean onCreateOptionsMenu (Menu menu Menu) {MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. menu, menu); return true;} public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. menu_one: Toast. makeText (this, you click menu_one, Toast. LENGTH_LONG ). show (); break; case R. id. submenu_one: Toast. makeText (this, you clicked submenu_one, Toast. LENGTH_LONG ). show (); break; default: return super. onOptionsItemSelected (item);} return true ;}}

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.