本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资源文件名称必须以小写开头,否则会报错。
运行效果
C#实现
using AndroID.App;using AndroID.OS;using AndroID.VIEws;using AndroID.Widget;namespace MenuDemo{ [Activity(Label = "@string/Applicationname",MainLauncher = true,Icon = "@drawable/Icon")] public class MainActivity : Activity { protected overrIDe voID OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentVIEw(Resource.Layout.Main); var iv = FindVIEwByID<ImageVIEw>(Resource.ID.iv); iv.SetimageResource(Resource.Drawable.test); RegisterForContextMenu(iv); } //与一个活动关联 public overrIDe bool OnCreateOptionsMenu(IMenu menu) { base.OnCreateOptionsMenu(menu); const int menuItemID = Menu.First; const int menuItemOrder = Menu.None; const int munuItemText = 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" ); var subMenu = menu.AddSubMenu(0,menuItemID + 30,menuItemOrder + 3,"子菜单" ); subMenu.SetheaderIcon(Resource.Drawable.more); var subMenuItem1 = subMenu.Add(0,menuItemID + 40,menuItemOrder + 4,"子菜单-1" ); subMenuItem1.SetIcon(Resource.Drawable.more); var subMenuItem2 = 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); return true; } public overrIDe bool OnoptionsItemSelected(IMenuItem item) { Toast.MakeText(this,"当前菜单编号" + item.ItemID,ToastLength.Long).Show(); return base.OnoptionsItemSelected(item); } public overrIDe voID OnCreateContextMenu(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,"菜单2").SetShortcut('2','B'); menu.Add(0,Menu.First + 2,"菜单3"); var subMenu = menu.AddSubMenu("子菜单"); subMenu.Add("子菜单选项"); } }}
Layout
<?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" androID:minWIDth="25px" androID:minHeight="25px"> <ImageVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:ID="@+ID/iv" /></linearLayout>
运行效果
Java实现
package com.example.halower.menudemo;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.ContextMenu;import androID.vIEw.MenuInflater;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ImageVIEw iv=(ImageVIEw)findVIEwByID(R.ID.iamgeVIEw); iv.setimageResource(R.drawable.test); registerForContextMenu(iv); } @OverrIDe public voID onCreateContextMenu(ContextMenu menu,ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu,menuInfo); MenuInflater inflater=new MenuInflater(this); inflater.inflate(R.menu.menu_main,menu); menu.setheaderIcon(R.drawable.blue); menu.setheaderTitle(R.string.menuTitle); }}
Layout
<?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>总结
以上是内存溢出为你收集整理的Android仿微信菜单(Menu)(使用C#和Java分别实现)全部内容,希望文章能够帮你解决Android仿微信菜单(Menu)(使用C#和Java分别实现)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)