search
HomeWeChat AppletWeChat DevelopmentShare an example of Android imitation WeChat menu implemented using C# and Java respectively

This article mainly introduces the Android imitation WeChat menu (Menu) (implemented using C# and Java respectively). This article gives the operating effects and implementation code of the C# and Java versions respectively. Friends in need can refer to the following

This article uses programming to implement Android menus. Of course, you can use XML to complete the same function. The basic Java and C# writing methods are the same, so using XML to demonstrate using Java in this article requires attention. Yes, if it is not developed by VS, the resource file name must start with lowercase, otherwise an error will be reported.

Running effect

C#realization


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(&#39;2&#39;,&#39;B&#39;);
      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>

Running effect

##Java implementation


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>

The above is the detailed content of Share an example of Android imitation WeChat menu implemented using C# and Java respectively. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor