Android 中文API (92) —— MenuInflater

前言

  本章内容是android.view.MenuInflater,版本为Android 2.3 r1,翻译来自"獨鍆躌踄",欢迎大家访问他的博客:http://www.cnblogs.com/mxgsa/,再次感谢"獨鍆躌踄" !期待你一起参与Android中文API的翻译,联系我over140@gmail.com。 

 

声明

  欢迎转载,但请保留文章原始出处:) 

    博客园:http://www.cnblogs.com/

    Android中文翻译组:http://goo.gl/6vJQl

 

正文

  一、结构

public class MenuInflater extends Object

        

java.lang.Object

android.view.MenuInflater

 

直接子类

TabActivity

 

  二、概述

    

  这个类是用来实例化菜单XML文件成菜单对象。

  由于性能的原因,由于程序创建时候就加载一些预处理XML文件,Menu过多就造成很重的负担。因此,这是目前无法在运行时使用多于一个XmlPullParser的xml文件去使用MenuInflater,它只能使用一个XmlPullParser返回的编译过的资源(R.某些文件)

 

  三、构造函数

           public MenuInflater (Context context)

  构造填充(inflater)一个菜单

参见

getMenuInflater()

 

  四、公共方法

           public void inflate (int menuRes, Menu menu)

  菜单层次从一个指定的xml资源去填充,如果有错误会抛掷InflateException

  参数

  menuRes         要加载XML布局文件中的资源ID(例如R.menu.main_activity

  menu       要填充的菜单,这些项目和子菜单就被添加到要填充菜单中

 

  五、补充

  文章精选

      MenuInflater Android菜单从xml创建方法

          Android中MenuInflater实例

          Android 中MenuInflater的使用(布局定义菜单)

  示例代码

  新建一个项目,项目文件列表

      

      MenuInfalterTest.java

public class MenuInflaterTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // 获取当前的菜单
        MenuInflater inflater = getMenuInflater();
        // 填充菜单
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    /**
     * 对菜单点击事件处理
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_add:
            break;
        case R.id.menu_wallaper:
            break;
        case R.id.menu_search:
            break;
        case R.id.menu_setting:
            showSettings();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 显示设置选项
     */
    private void showSettings() {
        Intent settings = new Intent
        (android.provider.Settings.ACTION_SETTINGS);
        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        startActivity(settings);
    }
}
}

      Main.xml

<?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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

      Option_menu.xml

<?xml version="1.0" encoding="utf-8"?>  
<menu xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:id="@+id/menu_add"  
        android:title="Add"  
        android:icon="@android:drawable/ic_menu_add"/>  
     <item android:id="@+id/menu_wallaper"  
        android:title="Wallpaper"  
        android:icon="@android:drawable/ic_menu_gallery"/>  
    <item  android:id="@+id/menu_search"  
        android:title="Search"  
        android:icon="@android:drawable/ic_search_category_default"/>  
    <item  android:id="@+id/menu_setting"  
        android:title="Settings"  
      android:icon="@android:drawable/ic_menu_preferences"/>
</menu> 

 

转载:http://www.cnblogs.com/over140/archive/2011/01/17/1937045.html

时间: 2024-08-30 13:13:26

Android 中文API (92) —— MenuInflater的相关文章

Android中文API(97)—— ContextMenu

前言 本章内容是android.view.ContextMenu,版本为Android 2.3 r1,翻译来自"Kun",再次感谢"Kun" !期待你一起参与Android 中文API的翻译,联系我over140@gmail.com.    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android中文翻译组:http://goo.gl/6vJQl   正文 一.结构 public interface Cont

Android 中文 API (100) —— ScrollView

前言 春节即至,谨代表Android中文翻译组全体同仁祝大家身体健康,工作顺利!从第一篇译稿2010年8月27发布至今天2011年1月27整5个月,共发布100篇译文,3个合集,在新的一年里,翻译组仍将坚持Android相关的翻译工作,秉承开源.合作.共享和坚持的信念打持久战,感谢大家的关心和支持! 本章内容是android.widget.ScrollView,版本为Android 2.3 r1,翻译来自"pengyouhong",再次感谢"pengyouhong"

Android 中文API (61) —— ViewSwitcher

前言 本章内容是 android.widget.ViewSwitcher,版本为Android 2.3 r1,翻译来自"ivanlee",再次感谢"ivanlee" !期待你一起参与Android中文API的翻译,联系我over140@gmail.com.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android中文翻译组:http://code.taobao.org/project/view/404/

Android 中文API (69) —— BluetoothAdapter[蓝牙]

前言 本章内容是 android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Android 2.3 r1,翻译来自中山大学的"Android Club SYSU",欢迎访问他们的WIKI:http://www.android-wiki.net,再次感谢"Android Club SYSU"!期待你一起参与Android中文API的翻译,联系我 over140

Android 中文API (70) —— BluetoothDevice[蓝牙]

前言 本章内容是 android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android 2.3 r1,翻译来自中山大学的"Android Club SYSU",欢迎访问他们的WIKI:http://www.android-wiki.net,再次感谢"Android Club SYSU"!期待你一起参与Android中文API的翻译,联系我 over140@gmail

android中文api(80)——Gallery.LayoutParams

前言 本章内容是 android.widget.Gallery.LayoutParams,版本为Android 2.3 r1,翻译来自"我是谁",欢迎大家访问他的博客:http://blog.sina.com.cn/u/1744311365,再次感谢"我是谁" !期待你加入Android 中文API的翻译,联系我over140@gmail.com.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android

Android 中文API (46) —— SimpleAdapter

前言 本章内容是 android.widget.SimpleAdapter,版本为Android 2.2 r1,翻译来自"德罗德",欢迎大家访问他的博客:http://sparkrico.javaeye.com/,再次感谢"德罗德" !期待你一起参与Android中文API的翻译,联系我over140@gmail.com.    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android中文翻译组:http:/

android中文api(79)——Gallery

前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www.blogjava.net/zlh320321,再次感谢"henly.zhang" !期待你加入Android 中文API的翻译,联系我over140@gmail.com.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android

Android 中文 API 文档 (45) —— AbsoluteLayout.LayoutParams

前言 本章内容是 android.widget.AbsoluteLayout.LayoutParams,版本为Android 2.2 r1,翻译来自"绵白糖",再次感谢"绵白糖" !期待你一起参与Android中文API的翻译,联系我over140@gmail.com.    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com/ Android中文翻译组:http://code.taobao.org/project/vi