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