Resources in Android refer to non-code parts, such as clips, audios, videos, and characters. Generally, we store native files in assets. For example, MP3 files cannot be directly accessed by Android programs. They must be read as binary streams through the AssetManager class. Layout files are generally stored in the res folder, such as anim, drawable, layout, values, xml, raw, and menu. These resources can be directly accessed through the R Resource class. Resources in assets are rarely used, and resources in res are often used.
Next we will continue to explain how to use these typical layout files:
7. Use menu resources: There are two ways to create any view component: one is to declare the creation in the layout file, and the other is to create a view component in the code, the same is true for menus.
Menus in Android include option menus, context menus, and sub-menus, which can be declared and defined in the XML file and used in the Code through the MenuInflater class.
1) typical menu resource file structure:
Root element, in Nested in the root element , Child element, The element can also be nested. Create a sub-menu.
2)
The root element has no attribute. It contains , Child Element
3) Indicates a menu group. You can set the attributes of the same menu, such as visable, enable, and checkable. The attributes are as follows:
Id: The reference id that uniquely identifies the menu group.
MenuCategory: classifies menus and defines the priority of menus. Valid values include container, system, secondary, and alternative.
OrderInCategory: A sort integer.
CheckableBehavior: Select behavior, single choice, multiple choice, or others. Valid values: none, all, and single.
Visable: whether visible, true or false.
Enabled: whether it is available, true or false.
4) Indicates a menu item, including
Or . The attributes of the element are as follows:
Id: The reference id that uniquely identifies the menu group.
MenuCategory: menu category.
OrderInCategory: A sort integer.
Title: A menu title string.
TitleCondensed: concentration title, suitable for use when the title is too long.
Icon: the icon of the menu.
AlphabeticShortcut: character shortcut.
NumberShortcut: Numeric shortcut.
Checkable: Optional.
Checked: whether the checked option is selected.
Visable: whether it is visible.
Enabled: whether it is available.
Case 1: Use menu. xml to create a menu
1) menu. xml file:
Android: icon = "@ drawable/ic_launcher"
Android: title = "File">
Android: id = "@ + id/noncheckable_group"
Android: checkableBehavior = "none">
Android: id = "@ + id/newFile"
Android: title = "New"
Android: alphabeticShortcut = "n">
Android: id = "@ + id/openFile"
Android: title = "Open"
Android: alphabeticShortcut = "o">
Android: id = "@ + id/saveFile"
Android: title = "Save"
Android: alphabeticShortcut = "s">
Android: title = "Edit"
Android: icon = "@ drawable/ic_launcher">
Android: id = "@ + id/edit_group"
Android: checkableBehavior = "single">
Android: id = "@ + id/cut"
Android: title = "Cut">
Android: id = "@ + id/past"
Android: title = "Past">
Android: title = "Help"
Android: icon = "@ drawable/ic_launcher">
Android: title = "About">
Android: title = "Exit">
Test_menu.java file:
Public class Test_Menu extends Activity {
Private MenuInflater mi;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. test_menu );
Mi = new MenuInflater (this );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Mi. inflate (R. menu. menu, menu );
Return true;
}
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. about:
AboutAlert ("This example shows how to use Xml menu resources to define menus! ");
Break;
Case R. id. exit:
ExitAlert ("are you sure you want to exit? ");
Break;
}
Return super. onOptionsItemSelected (item );
}
Private void exitAlert (String msg ){
// Instantiate AlertDialog. Builder
AlertDialog. Builder builder = new AlertDialog. Builder (this );
// Set the display information
Builder. setMessage (msg)
. SetCancelable (false)
// OK
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// End the Activity
Finish ();
}
// Cancel the button
}). SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Return;
}
});
// Create dialog box
AlertDialog alert = builder. create ();
// Display dialog box
Alert. show ();
}
Private void aboutAlert (String msg ){
// Instantiate AlertDialog. Builder
AlertDialog. Builder builder = new AlertDialog. Builder (this );
// Set the display information
Builder. setMessage (msg)
. SetCancelable (false)
// OK
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
}
});
// Create dialog box
AlertDialog alert = builder. create ();
// Display dialog box
Alert. show ();
}
}
Case 2: Create a menu directly in the Activity class:
Public class Option_Menu extends Activity {
// Constant of the menu item ID
Private static final int Item_Start = Menu. FIRST;
Private static final int Item_Exit = Menu. FIRST + 1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. option_menu );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Menu. add (0, Item_Start, 0, "Start ");
Menu. add (0, Item_Exit, 0, "exit ");
Return true;
}
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
// Menu item 1 Selected
Case Item_Start:
// This Activity title
SetTitle ("Start game ");
Break;
// Menu item 1 Selected
Case Item_Exit:
SetTitle ("quit! ");
Break;
}
Return true;
}
}
Source: http://blog.csdn.net/cl05300629/article/details/17717573 Author: Standing