本篇是对安卓菜单使用编程方式实现,当然可以使用xml的方式完成同样的功能,基本java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资源文件名称必须以小写开头,否则会报错。
运行效果
C#实现usingAndroid.App;usingAndroid.OS;usingAndroid.Views;usingAndroid.Widget;namespaceMenuDemo
{
[Activity(Label="@string/applicationName", MainLauncher =true, Icon ="@drawable/Icon")]publicclassMainActivity : Activity
{PRotectedoverridevoidOnCreate(Bundle bundle)
{base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);variv = FindViewById<ImageView>(Resource.Id.iv);
iv.SetImageResource(Resource.Drawable.test);
RegisterForContextMenu(iv);
}//与一个活动关联publicoverrideboolOnCreateOptionsMenu(IMenu menu)
{base.OnCreateOptionsMenu(menu);constintmenuItemId =Menu.First;constintmenuItemOrder =Menu.None;constintmunuItemText =Resource.String.menuItem1;/** 第一个参数:组值
* 第二个参数:菜单唯一标示
* 第三个参数:顺序参数
* 第四个参数:文本*/menu.Add(1, menuItemId, menuItemOrder, munuItemText);
menu.Add(2, menuItemId +10, menuItemOrder +1,"菜单2");
menu.Add(2, menuItemId +20, menuItemOrder +2,"菜单3");varsubMenu = menu.AddSubMenu(0, menuItemId +30, menuItemOrder +3,"子菜单");
subMenu.SetHeaderIcon(Resource.Drawable.more);varsubMenuItem1 = subMenu.Add(0, menuItemId +40, menuItemOrder +4,"子菜单-1");
subMenuItem1.SetIcon(Resource.Drawable.more);varsubMenuItem2 = subMenu.Add(0, menuItemId +50, menuItemOrder +5,"子菜单-2").SetCheckable(true);
subMenuItem2.SetIcon(Resource.Drawable.blue);
subMenu.Add(0, menuItemId +60, menuItemOrder +6,"子菜单-3").SetCheckable(true);returntrue;
}publicoverrideboolOnOptionsItemSelected(IMenuItem item)
{
Toast.MakeText(this,"当前菜单编号"+item.ItemId, ToastLength.Long).Show();returnbase.OnOptionsItemSelected(item);
}publicoverridevoidOnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
{base.OnCreateContextMenu(menu, v, menuInfo);
menu.SetHeaderTitle("上下文菜单");
menu.SetHeaderIcon(Resource.Drawable.blue);
menu.Add(0, Menu.First, Menu.None,"菜单1");
menu.Add(0, Menu.First+1,Menu.None,"菜单2").SetShortcut('2','B');
menu.Add(0, Menu.First +2, Menu.None,"菜单3");varsubMenu = menu.AddSubMenu("子菜单");
subMenu.Add("子菜单选项");
}
}
}
Layout<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:minWidth="25px"android:minHeight="25px"><ImageViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/iv"/></LinearLayout>
运行效果
Java实现packagecom.example.halower.menudemo;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.ContextMenu;importandroid.view.MenuInflater;importandroid.view.View;importandroid.widget.ImageView;publicclassMainActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv=(ImageView)findViewById(R.id.iamgeView);
iv.setImageResource(R.drawable.test);
registerForContextMenu(iv);
}
@OverridepublicvoidonCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater=newMenuInflater(this);
inflater.inflate(R.menu.menu_main,menu);
menu.setHeaderIcon(R.drawable.blue);
menu.setHeaderTitle(R.string.menuTitle);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MenuDemo</string>
<string name="action_settings">Settings</string>
<string name="item1Name" >菜单1</string>
<string name="item2Name" >菜单2</string>
<string name="item3Name" >菜单3--More</string>
<string name="item4Name" >菜单4</string>
<string name="menuTitle">XML中解析菜单</string>
<string name="subitem1Name">子菜单1</string>
<string name="subitem2Name">子菜单2</string>
</resources>