zoukankan      html  css  js  c++  java
  • 【转】android小结(一)之menu

    原文网址:http://zhouyunan2010.iteye.com/blog/1151215

    android提供有三种menu类型 
    一.Options Menu(选项菜单) 
    这是一组item选项的集合,当用户点击MENU button时,此menu就会出现。如果是3.0以后的版本可以通过action bar直接选择menu item 
    二.Context Menu(上下文菜单) 
    当用户长按住一个注册了上下文菜单的控件时,会弹出一个上下文菜单,它是一个流式的列表,供用户选择某项 
    三.Submenu(子菜单) 
    一个item项可以包含一个内嵌的子菜单 


    1.下面讲如何创建一个Options Menu菜单 
    通过xml布局文件创建菜单项 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <item android:id="@+id/new_game"  
    4.           android:icon="@drawable/ic_new_game"       <!--菜单图标-->  
    5.           android:title="@string/new_game" />        <!--菜单文字-->  
    6.     <item android:id="@+id/help"  
    7.           android:icon="@drawable/ic_help"  
    8.           android:title="@string/help" />  
    9. </menu>  


    调用:重写acitvity的onCreateOptionsMenu方法创建菜单 

    Java代码  收藏代码
    1. @Override  
    2. public boolean onCreateOptionsMenu(Menu menu) {  
    3.     MenuInflater inflater = getMenuInflater();  
    4.     inflater.inflate(R.menu.game_menu, menu);  
    5.     return true;  
    6. }  


    你也可以直接调用menu的add方法直接添加菜单项。 

    options menu响应用户事件需重写activity的onOptionsItemSelected 

    Java代码  收藏代码
    1. @Override  
    2. public boolean onOptionsItemSelected(MenuItem item) {  
    3.     // Handle item selection  
    4.     switch (item.getItemId()) {  
    5.     case R.id.new_game:  
    6.         newGame();  
    7.         return true;  
    8.     case R.id.help:  
    9.         showHelp();  
    10.         return true;  
    11.     default:  
    12.         return super.onOptionsItemSelected(item);  
    13.     }  
    14. }  


    根据item id分别对选中某选项进行处理。return true表示自己已经处理此方法,不需要父类onOptionsItemSelected进行处理。 
    如果你有多个activity都有相同的menu,可以写一个activity实现onCreateOptionsMenu() and onOptionsItemSelected() 方法,然后其他activity继承此activity 

    在运行时改变menu item的状态 
    你需要重写onPrepareOptionsMenu() ,它方便你对item移除,添加,使不可用等 
    在android 2.3以下,当用户要每次打开menu时就会调用onPrepareOptionsMenu()方法 
    在android 3.0以上,你必须调用invalidateOptionsMenu() 当你要update你的menu时,因为 
    action bar是一直出现的。然后系统将调用onPrepareOptionsMenu()更改menu 


    2.创建一个Context Menu 
    上下文菜单相当于pc机上的点击鼠标右键弹出框。当用户长按一选项时,上下文菜单就会出现。你能为任何view创建一个上下文菜单,尽管Context Menu多用于listview。 
    要创建一个Context Menu,你首先必须为你的控件注册registerForContextMenu() ,并重写 
    onCreateContextMenu() and onContextItemSelected(). 

    Java代码  收藏代码
    1. @Override  
    2. public void onCreateContextMenu(ContextMenu menu, View v,  
    3.                                 ContextMenuInfo menuInfo) {  
    4.   super.onCreateContextMenu(menu, v, menuInfo);  
    5.   MenuInflater inflater = getMenuInflater();  
    6.   inflater.inflate(R.menu.context_menu, menu);  
    7. }  


    这里使用布局文件创建菜单项,跟options menu差不多。当然你同样可以使用menu.add()方法创建items。ContextMenu.ContextMenuInfo 对象将提供你选择的选项的额外信息,比如id 

    Java代码  收藏代码
    1. @Override  
    2. public boolean onContextItemSelected(MenuItem item) {  
    3.   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  
    4.   switch (item.getItemId()) {  
    5.   case R.id.edit:  
    6.     editNote(info.id);  
    7.     return true;  
    8.   case R.id.delete:  
    9.     deleteNote(info.id);  
    10.     return true;  
    11.   default:  
    12.     return super.onContextItemSelected(item);  
    13.   }  
    14. }  




    3.创建submenu 
    A submenu is a menu that the user can open by selecting an item in another menu。 
    一个子菜单能够从另外一个菜单的选项中被打开。你能为任何一个menu添加子菜单,除了submenu。下面看看如何通过布局文件创建一个submenu 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <item android:id="@+id/file"  
    4.           android:icon="@drawable/file"  
    5.           android:title="@string/file" >  
    6.         <!-- "file" submenu -->  
    7.         <menu>  
    8.             <item android:id="@+id/create_new"  
    9.                   android:title="@string/create_new" />  
    10.             <item android:id="@+id/open"  
    11.                   android:title="@string/open" />  
    12.         </menu>  
    13.     </item>  
    14. </menu>  



    You can also use addSubMenu() to dynamically add a SubMenu to an existing Menu. This returns the new SubMenu object, to which you can add submenu items, using add() 

    4.其他的menu特性 

    Menu groups
     
    通过组菜单,你可以同时对一组item进行操作,比如setGroupVisible(),setGroupEnabled(),setGroupCheckable() 
    下面给出一个包含组菜单的资源文件 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <item android:id="@+id/item1"  
    4.           android:icon="@drawable/item1"  
    5.           android:title="@string/item1" />  
    6.     <!-- menu group -->  
    7.     <group android:id="@+id/group1">  
    8.         <item android:id="@+id/groupItem1"  
    9.               android:title="@string/groupItem1" />  
    10.         <item android:id="@+id/groupItem2"  
    11.               android:title="@string/groupItem2" />  
    12.     </group>  
    13. </menu>  


    3个item任然是一个兄弟菜单,不同的是你可以通过组操作一组item 

    Checkable menu items
     
    通过布局文件创建Checkable menu,可以通过使用item的android:checkable属性 
    和group的android:checkableBehavior属性。例如 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <group android:checkableBehavior="single">  
    4.         <item android:id="@+id/red"  
    5.               android:title="@string/red" />  
    6.         <item android:id="@+id/blue"  
    7.               android:title="@string/blue" />  
    8.     </group>  
    9. </menu>  


    APi中: 
    The android:checkableBehavior attribute accepts either: 
    single 
        Only one item from the group can be checked (radio buttons) 
    all 
        All items can be checked (checkboxes) 
    none 
        No items are checkable 
    由于不会自动改变checkbox的状态,你必须设置checkbox的状态通过isChecked()和setChecked(). 

    Java代码  收藏代码
    1. @Override  
    2. public boolean onOptionsItemSelected(MenuItem item) {  
    3.   switch (item.getItemId()) {  
    4.   case R.id.vibrate:  
    5.   case R.id.dont_vibrate:  
    6.     if (item.isChecked()) item.setChecked(false);  
    7.     else item.setChecked(true);  
    8.     return true;  
    9.   default:  
    10.     return super.onOptionsItemSelected(item);  
    11.   }  
    12. }  


    如果你不手动设置它,当你点击一个选项时,checkbox或者radioButton的状态是不会改变的 

    Shortcut keys(热键) 
    你可以为你item设置快速点击热键 
    在配置文件中通过item的属性android:alphabeticShortcut 和 android:numericShortcut 
    类文件中通过setAlphabeticShortcut(char) and setNumericShortcut(char).设置的热键不区分大小写 


    Dynamically adding menu intents 
    有时候你希望使用menu item来启动一个activity通过intent。你能在onOptionsItemSelected()使用startActivity() 
    然而如果你不确定用户的移动设备是否拥有一个应用程序来处理这个intent,那么你可以使用Dynamically adding menu intents来决定是否添加此item 
    一个简单的实例 

    Java代码  收藏代码
    1. @Override  
    2. public boolean onCreateOptionsMenu(Menu menu){  
    3.     super.onCreateOptionsMenu(menu);  
    4.   
    5.     // Create an Intent that describes the requirements to fulfill, to be included  
    6.     // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.  
    7.     Intent intent = new Intent(null, dataUri);  
    8.     intent.addCategory(Intent.CATEGORY_ALTERNATIVE);  
    9.   
    10.     // Search and populate the menu with acceptable offering applications.  
    11.     menu.addIntentOptions(  
    12.          R.id.intent_group,  // Menu group to which new items will be added  
    13.          0,      // Unique item ID (none)  
    14.          0,      // Order for the items (none)  
    15.          this.getComponentName(),   // The current activity name  
    16.          null,   // Specific items to place first (none)  
    17.          intent, // Intent created above that describes our requirements  
    18.          0,      // Additional flags to control items (none)  
    19.          null);  // Array of MenuItems that correlate to specific items (none)  
    20.   
    21.     return true;  
    22. }  


    addIntentOptions()返回的是你添加的item的个数 

    Allowing your activity to be added to other menus 
    如果允许你的activity可以被别的menu添加,必须设置CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE values for the intent filter category 

    Xml代码  收藏代码
      1. <intent-filter label="Resize Image">  
      2.     ...  
      3.     <category android:name="android.intent.category.ALTERNATIVE" />  
      4.     <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />  
      5.     ...  
      6. </intent-filter>  
      7.   
      8. 还有一个notePad的示例我在研究研究  
  • 相关阅读:
    Django---分页器
    Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)
    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
    python web框架简介Bottle Flask Tornado
    [C#] 谈谈异步编程async await
    MYSQL分库分表总结
    win10 IIS 10.0 无法安装 URL Rewrite Module 重写模块
    Quartz.NET文档 入门教程
    SignalR web实时同步 消息推送 广播
    C#模拟http 发送post或get请求
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4488028.html
Copyright © 2011-2022 走看看