Android creates menus through XML

Source: Internet
Author: User

Generally, the developer defines the GUI of the application in the res/Layout path. Apply Eclipse
After creating a new project, you can see that a preset main. xml file exists in res/layout.
As the default startup interface of the program. Similarly, you can create a static Menu in this way.
For the method, see the source code below:
? View Code XML
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item
Android: id = "@ + id/previous"
Android: title = "@ string/previous"
Android: enabled = "false"
Android: icon = "@ android: drawable/ic_media_previous"/>
<! -- These may not be available in next api (level> 3), so be carefull -->
<Item
Android: id = "@ + id/play_pause"
Android: title = "@ string/play"
Android: icon = "@ android: drawable/ic_media_play"/>
<Item
Android: id = "@ + id/next"
Android: title = "@ string/next"
Android: icon = "@ android: drawable/ic_menu_next"/>
</Menu>
Call the created Menu in the Activity class. First, associate the current Activity with the specified Menu XML:
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Super. onCreateOptionsMenu (menu );
GetMenuInflater (). inflate (R. layout. menu_mainactivity, menu );
Return true;
}
OnOptionsItemSelected:
Sends the option to make a response, and the actually called function is included in the respective case)
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. previous:
Previous (); // go to previous song in the playlist
Return true;
Case R. id. play_pause:
IsPlaying ()? Pause (): play (); // toggle play/pause
Return true;
Case R. id. next:
Next (); // go to next song in the playlist
Return true;
}
Return false; // shocould never happen
}
Finally, you can use the onprepareoptionmenu method to initialize the menu items attributes:
@ Override
Public Boolean onprepareoptionsmenu (menu ){
// Set play_pause menu item look
If (isplaying ()){
Menu
. Finditem (R. Id. play_pause)
. Settitle (R. String. Pause)
. Seticon (Android. R. drawable. ic_media_pause );
} Else {
Menu
. Finditem (R. Id. play_pause)
. Settitle (R. String. Play)
. Seticon (Android. R. drawable. ic_media_play );
}
Return true;
}

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.