• Android


    一、OptionMenu (选项菜单)

    简介:按下menu键时,弹出此菜单,也是最常见的菜单

    二、ContextMenu (上下文菜单)

    简介:桌面系统中有右键,可移动不行啊,所以就有了长按事件,以代替右键菜单

    三、 PopuMenu (弹出式菜单)

    简介:顾明思意,就是弹出式菜单了,便于交互

    布局如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <menu xmlns:android="http://schemas.android.com/apk/res/android"  >
     3         <item
     4             android:id="@+id/menu_1_1"
     5             android:icon="@drawable/ic_launcher"
     6             android:title="一号" android:visible="true">
     7         </item>
     8         <item
     9             android:id="@+id/menu_1_2"
    10             android:icon="@drawable/ic_launcher"
    11             android:title="二号">
    12             <menu>
    13                 <item
    14                     android:id="@+id/menu_2_1"
    15                     android:icon="@drawable/ic_launcher"
    16                     android:title="二号一号">
    17                      <menu>
    18                         <item
    19                             android:id="@+id/menu_2_2_1"
    20                             android:icon="@drawable/ic_launcher"
    21                             android:title="一号">
    22                         </item>
    23                         <item
    24                             android:id="@+id/menu_2_2_2"
    25                             android:title="二号">
    26                         </item>
    27                         <item
    28                             android:id="@+id/menu_2_2_3"
    29                             android:title="三号">
    30                         </item>
    31                     </menu>
    32                 </item>
    33                 <item
    34                     android:id="@+id/menu_2_2"
    35                     android:title="二号">
    36                 </item>
    37                 <item
    38                     android:id="@+id/menu_2_3"
    39                     android:title="三号">
    40                 </item>
    41             </menu>
    42         </item>
    43         <item
    44             android:id="@+id/menu_1_3"
    45             android:icon="@drawable/ic_launcher"
    46             android:title="三号">
    47         </item>
    48        
    49 </menu>
    布局代码

    应用如下:

      1 package lilonghe.example.menudemo;
      2 
      3 import java.io.Console;
      4 import java.lang.reflect.Array;
      5 import java.lang.reflect.Method;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import android.R.array;
     10 import android.app.Activity;
     11 import android.location.Address;
     12 import android.os.Bundle;
     13 import android.util.Log;
     14 import android.view.ContextMenu;
     15 import android.view.Menu;
     16 import android.view.MenuItem;
     17 import android.view.SubMenu;
     18 import android.view.View;
     19 import android.view.ContextMenu.ContextMenuInfo;
     20 import android.widget.ArrayAdapter;
     21 import android.widget.ListView;
     22 import android.widget.PopupMenu;
     23 
     24 public class MainActivity extends Activity {
     25 
     26     private ListView listView;
     27     @Override
     28     protected void onCreate(Bundle savedInstanceState) {
     29         super.onCreate(savedInstanceState);
     30         setContentView(R.layout.activity_main);
     31         listView = (ListView)findViewById(R.id.listView1);
     32         String[] arrayStrings = new String[]{"一号","二号"};
     33         ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,arrayStrings);
     34         listView.setAdapter(adapter);
     35         // 注册“上下文菜单”
     36         registerForContextMenu(listView);
     37     }
     38     
     39     public void showPopuMenu(View v) {
     40         
     41         PopupMenu pmMenu = new PopupMenu(this, v);
     42         pmMenu.getMenuInflater().inflate(R.menu.main_menu, pmMenu.getMenu());
     43         setIconEnable(pmMenu.getMenu(),true);
     44         pmMenu.show();
     45     }
     46     
     47     /**
     48      * 创建上下文菜单
     49      */
     50     @Override
     51     public void onCreateContextMenu(ContextMenu menu, View v,
     52             ContextMenuInfo menuInfo) {
     53         // TODO Auto-generated method stub
     54         super.onCreateContextMenu(menu, v, menuInfo);
     55         setIconEnable(menu,true);
     56         getMenuInflater().inflate(R.menu.main_menu, menu);
     57         menu.setHeaderTitle("this is title");
     58         
     59     }
     60     /**
     61      * 上下文菜单选中
     62      */
     63     @Override
     64     public boolean onContextItemSelected(MenuItem item) {
     65         // 同OptionMenu
     66         return super.onContextItemSelected(item);
     67     }
     68     /**
     69      * 创建选项菜单
     70      */
     71     @Override
     72     public boolean onCreateOptionsMenu(Menu menu) {
     73         setIconEnable(menu, true);
     74         // 第一种:布局文件
     75         //getMenuInflater().inflate(R.menu.main_menu, menu);
     76         // 第二种:动态创建
     77         menu.add(1, 1, 1, "首页");
     78         menu.getItem(0).setIcon(R.drawable.ic_launcher);
     79         menu.add(1, 2, 1, "二页");
     80         menu.getItem(1).setIcon(R.drawable.ic_launcher);
     81         menu.add(1, 3, 1, "三页");
     82         menu.getItem(2).setIcon(R.drawable.ic_launcher);
     83         //二级菜单
     84         SubMenu subMenu = menu.addSubMenu("设置性别");
     85         subMenu.setIcon(R.drawable.ic_launcher);
     86         subMenu.add(2,5,2,"女");
     87         SubMenu s_2 = subMenu.addSubMenu("男");
     88         s_2.setHeaderTitle("真是男的?");
     89         s_2.add(1, 6, 1, "真的是的");
     90         s_2.add(1,7,1,"不告诉你");    
     91         return true;
     92     }
     93     /***
     94      * 此方法解决Android4.0以上不显示图标问题
     95      * @param menu
     96      * @param b
     97      */
     98     private void setIconEnable(Menu menu, boolean b) {
     99         try 
    100         {
    101             Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
    102             Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
    103             m.setAccessible(true);
    104         
    105             //MenuBuilder实现Menu接口,创建菜单时,传进来的menu其实就是MenuBuilder对象(java的多态特征)
    106             m.invoke(menu, true);    
    107         } catch (Exception e){
    108             e.printStackTrace();
    109         }
    110     }
    111     /**
    112      * 选项菜单选中
    113      */
    114     @Override
    115     public boolean onOptionsItemSelected(MenuItem item) {
    116         // TODO Auto-generated method stub
    117         Log.i("ItemId", item.getItemId()+"");
    118         Log.i("ItemTitle", item.getTitle().toString());
    119         switch(item.getItemId()){
    120             case R.id.menu_1_1:
    121                 Log.i("switch", "1_1");
    122             break;
    123             case R.id.menu_1_2:
    124                 Log.i("switch", "1_2");
    125             break;
    126             case R.id.menu_1_3:
    127                 Log.i("switch", "1_3");
    128             break;
    129         }
    130         //获取ID: item.getItemId();
    131         //获取标题:item.getTitle();
    132         return true;
    133     }
    134 }
    应用代码
  • 相关阅读:
    BZOJ4152: [AMPPZ2014]The Captain
    BZOJ4025: 二分图
    BZOJ1453: [Wc]Dface双面棋盘
    BZOJ3238: [Ahoi2013]差异
    BZOJ3165: [Heoi2013]Segment
    BZOJ4556: [Tjoi2016&Heoi2016]字符串
    BZOJ2668: [cqoi2012]交换棋子
    UVa-10652 包装木板
    HDU1599-Find the mincost route
    HDU-3339 IN ACTION(Dijkstra +01背包)
  • 原文地址:https://www.cnblogs.com/hualongbei/p/4148513.html
Copyright © 2020-2023  润新知