Loading

安卓开发学习之Menu

安卓开发中菜单是一个很重要的组件,从安卓开发文档(http://wear.techbrood.com/guide/index.html)中可以看到,安卓UI设计中的Menu主要分为:

A.Options menu and action bar

     

 在3.0以下的系统中,Options menu 显示为左图的效果,接下来看看怎么在app中实现一个options menu。

静态 menu 可以从 xml 文件中直接加载,所以我们要先写一个 menu 列表

1.编写 xml 文件, 如 main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.testmenu.MainActivity" >

    <item
        android:id="@+id/add"
        android:title="add"/>
    
    <item
        android:id="@+id/delete"
        android:title="delete"/>
    
    <item
        android:id="@+id/query"
        android:title="query"/>
    
    <item
        android:id="@+id/update"
        android:title="update"/>

</menu>

 

 2.重写 onCreateOptionsMenu() 方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

 

3.处理 menu 的交互(在例子中使用 Toast 弹出信息确定点击了哪个 item )

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    Toast.makeText(MainActivity.this, "You click " + item.getTitle(), Toast.LENGTH_SHORT).show();
}

 

 以上就完成了一个 menu 的简单例子。

另:有些情况下,我们需要在程序运行时动态生成一个 menu ,这种情况下自然就不适合使用 xml 文件来加载 menu 了,官方开发文档告诉我们,

After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.

 

也就是说正常情况下系统调用了 onCreateOptionsMenu() 方法以后不会再次调用该方法,这个方法只是用来初始化 menu 的,当你需要动态修改 menu 的情况下就不应该使用这个方法了。

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents. See Activity.onPrepareOptionsMenu for more information.

很清楚, menu 每次被显示出来之前, onPrepareOptionsMenu() 都会被调用一次,所以如果我们要生成动态 menu 的话,处理 menu 的代码就应该写在这里。

例如:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add("hello");
    return super.onPrepareOptionsMenu(menu);
}

 

在例子中我们重载了 onPrepareOptionsMenu() 函数,效果是每点击一次 menu ,都会在 menu 中添加一个 hello 的菜单项。

 

 

B.Context menu and contextual action mode

 

左边是Context menu

 

1.为 Context menu 注册 view 对象,也就是通过哪个对象来激活 menu

registerForContextMenu(textView);

 

2.实现 onCreateContextMenu() 方法

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    getMenuInflater().inflate(R.menu.main, menu);
}

 

3.实现 onContextItemSelected(MenuItem item) 方法

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "You click " + item.getTitle(), Toast.LENGTH_SHORT).show();
    return super.onContextItemSelected(item);
}

 

4.实现 ActionMode.Callback 接口

private ActionMode.Callback mCallback = new ActionMode.Callback() {
        
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }
        
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();;
            return false;
        }
    };

 

5.通过监听器调用 startActionMode(mCallback) 方法,如:

TextView textView = (TextView) findViewById(R.id.hello);
textView.setOnClickListener(new OnClickListener() {
            
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActionMode(mCallback);
    }
});

 

 

C.Popup menu

 Popup menu 与上面两种 menu 基本相同,可以参考http://wear.techbrood.com/guide/topics/ui/menus.html

posted @ 2016-07-19 13:37  土豆分米猪  阅读(954)  评论(0编辑  收藏  举报