Android中使用XML文件配置OptionMenu

标签: Android SDK

[1].[代码] menu.xml 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<? xml version = "1.0" encoding = "utf-8" ?>
< menu xmlns:Android = "http://schemas.android.com/apk/res/android" >
 
     < item Android:id = "@+id/close" android:title = "Close"
         Android:orderInCategory = "3" android:icon = "@drawable/eject" />
     < item Android:id = "@+id/no_icon" android:orderInCategory = "2"
         Android:title = "Sans Icon" />
     < item Android:id = "@+id/disabled" android:orderInCategory = "4"
         Android:enabled = "false" android:title = "Disabled" />
     < group Android:id = "@+id/other_stuff" android:menuCategory = "secondary"
         Android:visible = "false" >
         < item Android:id = "@+id/later" android:orderInCategory = "0"
             Android:title = "2nd-To-Last" />
         < item Android:id = "@+id/last" android:orderInCategory = "1"
             Android:title = "Last" />
     </ group >
     < item Android:id = "@+id/submenu" android:orderInCategory = "3"
         Android:title = "A Submenu" >
         < menu >
             < item Android:id = "@+id/non_ghost" android:title = "Non-Ghost"
                 Android:visible = "true" android:alphabeticShortcut = "n" />
             < item Android:id = "@+id/ghost" android:title = "A Ghost"
                 Android:visible = "false" android:alphabeticShortcut = "g" />
         </ menu >
     </ item >
</ menu >

[2].[代码] main.xml 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:Android = "http://schemas.android.com/apk/res/android"
     Android:orientation = "vertical"
     Android:layout_width = "fill_parent"
     Android:layout_height = "fill_parent"
     >
 
</ LinearLayout >

[3].[代码] XmlMenuAcitvity.java 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package yyl.xmlMenu;
 
import Android.app.Activity;
import Android.os.Bundle;
import Android.view.ContextMenu;
import Android.view.ContextMenu.ContextMenuInfo;
import Android.view.Menu;
import Android.view.MenuInflater;
import Android.view.View;
 
public class XmlMenuAcitvity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
 
         //将页面定义的menu配置到到activity中
         new MenuInflater(getApplication())
         .inflate(R.layout.menu, menu);
         return super .onCreateOptionsMenu(menu);
     }
 
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值