Android menu + anctionbar
I. Overview
Menu is the window that pops up when you press the "menu" Key of your mobile phone. It is widely used and is available in almost every application.
Ii. Requirements
Menu functions are implemented in two ways.
Iii. Implementation
Create a project MyMenu and modify the/res/layout/main. xml file. The complete main. xml file is as follows:
1
2
3 android: layout_width = "fill_parent"
4 android: layout_height = "fill_parent"
5 android: orientation = "vertical">
6
7
8 android: layout_width = "fill_parent"
9 android: layout_height = "wrap_content"
10 android: text = "this is a menu test program"
11 android: gravity = "center_horizontal"
12/>
13
14
Next, modify MyMenuActivity. the java file mainly overwrites the onCreateOptionsMenu () and onOptionsItemSelected () methods. It is relatively simple to add seven options to the onCreateOptionsMenu () method. The complete content is as follows:
1 package com. nan. menu;
2
3 import android. app. Activity;
4 import android. OS. Bundle;
5 import android. view. Menu;
6 import android. view. MenuItem;
7 import android. widget. Toast;
8
9 public class MyMenuActivity extends Activity
10 {
11/** Called when the activity is first created .*/
12 @ Override
13 public void onCreate (Bundle savedInstanceState)
14 {
15 super. onCreate (savedInstanceState );
16 setContentView (R. layout. main );
17
18}
19
20 @ Override
21 // execute this function when the "menu" button is pressed
22 public boolean onCreateOptionsMenu (Menu menu)
23 {
24 menu. add (0, 0, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
25 menu. add (0, 1, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
26 menu. add (0, 2, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
27 menu. add (0, 3, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
28 menu. add (0, 4, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
29 menu. add (0, 5, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
30 menu. add (0, 6, Menu. NONE, "menu"). setIcon (android. R. drawable. btn_star );
31
32 return super. onCreateOptionsMenu (menu );
33}
34
35
36 // execute this function when an item in the menu is selected
37 @ Override
38 public boolean onOptionsItemSelected (MenuItem item)
39 {
40 switch (item. getItemId ())
41 {
42 case 0: displayToast ("0th selected"); break;
43 case 1: displayToast ("1st items selected"); break;
44 case 2: displayToast ("2nd items selected"); break;
45 case 3: displayToast ("3rd items selected"); break;
46 case 4: displayToast ("4th items selected"); break;
47 case 5: displayToast ("5th items selected"); break;
48 case 6: displayToast ("6th items selected"); break;
49
50}
51
52 return super. onOptionsItemSelected (item );
53}
54
55 /*
56 @ Override
57 public boolean onCreateOptionsMenu (Menu menu)
58 {
59 MenuInflater inflater = getMenuInflater ();
60 inflater. inflate (R. layout. menu, menu); menu. add (0, 0, Menu. NONE, "1 "). setIcon (R. drawable. ic_launcher); // Add other items by yourself
61 return super. onCreateOptionsMenu (menu );
62}
63
64 @ Override
65 public boolean onOptionsItemSelected (MenuItem item)
66 {
67 int item_id = item. getItemId ();
68
69 switch (item_id)
70 {
71 case R. id. mlist: // custom
72 {}
73}
74 return super. onOptionsItemSelected (item );
75}
76 */
77
78
79
80 // display the Toast Function
81 private void displayToast (String s)
82 {
83 Toast. makeText (this, s, Toast. LENGTH_LONG). show ();
84}
85
86}
You can remove comments and replace the Code with XML to construct the menu.
The name is menu.
-
-
- Android: icon = "@ drawable/ic_action_search"
- Android: title = "@ string/action_search"
- Android: showAsAction = "ifRoom"/>
-
- Android: title = "@ string/action_settings"
- Android: showAsAction = "never"/>
I don't know why the icons are shown in the list.
PS: No icon is allowed only in the list and not in the actionbar.