Android仿微信菜单(Menu)(使用C#和Java分别实现)
程序员文章站
2022-06-10 19:06:55
本篇是对安卓菜单使用编程方式实现,当然可以使用xml的方式完成同样的功能,基本java和c#写法都是一致的,所以使用xml的方式在本篇中使用java演示,需要注意的是,对于...
本篇是对安卓菜单使用编程方式实现,当然可以使用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,menu.none, "菜单2").setshortcut('2','b'); menu.add(0, menu.first + 2, menu.none, "菜单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, view v, contextmenu.contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, 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>