Android总结 - Menu

showAsAction参数

never:不以button的形式在ActionBar显示出来。

ifRoom:如果系统觉得ActionBar上有足够的空间就以button的形式显示,没有空间就不以button形式显示。

always:总是以button的形式在ActionBar上显示。以往的经验来看,一次显示最好不要超过两个。

withText:总是以文字来显示,不管有没有设置icon。

collapseActionView: item的action view被折叠到一个按钮中,当用户选择这个按钮时,这个操作视窗展开。

可以通过 item 的”android:orderInCategory “属性来进行Menu item的重新排序

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- These are in reverse order in this resource, but the orderInCategory attribute will
         order them for the menu (they all have the same default category). -->
    <item android:title="forth item"
            android:orderInCategory="3"/>
    <item android:title="third item"
            android:orderInCategory="2"/>
    <item android:title="second item"
            android:orderInCategory="1"/>
    <item android:title="first item"
            android:orderInCategory="0"/>
</menu>

在运行中修改menu items

在Android3.0及以上的版本上,当出现一个事件并且你想要执行menu的更新动作,可以通过调用“invalidateOptionsMenu() ”来请求系统重新调用“onPrepareOptionsMenu()”从而来更换MenuItem。

Using the contextual action mode

为单个视图启用上下文操作模式

  1. 实现 ActionMode.Callback 接口。在这个callback方法中,你可以指定 Icontextual action bar的响应动作, 响应每一个action items的click事件,并且控制action mode的其他他生命周期事件。
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_share:
                shareCurrentItem();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};
  1. 当你想要显示ActionMode bar的时候(比如长按View),调用 startActionMode() 。
someView.setOnLongClickListener(new View.OnLongClickListener() {
    // Called when the user long-clicks on someView
    public boolean onLongClick(View view) {
        if (mActionMode != null) {
            return false;
        }

        // Start the CAB using the ActionMode.Callback defined above
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        view.setSelected(true);
        return true;
    }
});

在 ListView 或 GridView 中启用批处理上下文操作

  1. 实现 AbsListView.MultiChoiceModeListener 接口,并使用 setMultiChoiceModeListener() 为视图组设置该接口。在侦听器的回调方法中,您既可以为上下文操作栏指定操作,也可以响应操作项目的点击事件,还可以处理从 ActionMode.Callback 接口继承的其他回调。
  2. 使用 CHOICE_MODE_MULTIPLE_MODAL 参数调用 setChoiceMode()。
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

创建Menu Groups

Menu group是Menu items的组合具有相同的特性。 通过Menu Groups可以做以下事务:
- 使用 setGroupVisible() 显示或隐藏所有项目
- 使用 setGroupEnabled() 启用或禁用所有项目
- 使用 setGroupCheckable() 指定所有项目是否可选中

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_save"
          android:icon="@drawable/menu_save"
          android:title="@string/menu_save" />
    <!-- menu group -->
    <group android:id="@+id/group_delete">
        <item android:id="@+id/menu_archive"
              android:title="@string/menu_archive" />
        <item android:id="@+id/menu_delete"
              android:title="@string/menu_delete" />
    </group>
</menu>

使用可选中的菜单项

Checkable items 只可以在submenus或者context menus中使用。

要使用menu的checkable功能,可以通过设置item的android:checkable或者设置一个group的android:checkableBehavior值,android:checkableBehavior有三个值可以选择:single(单选)、all(多选)、none(不能选)。
当点击了选择menu item之后,需要自己手动写代码来改变item的选择状态,因为系统不会自动修改并保存。如果想要持久化,可以使用Shared Preferences。

    <item android:title="All">
        <menu>
            <group android:id="@+id/checkable_group"
                   android:checkableBehavior="all">
                <item android:id="@+id/checkable_item_1"
                      android:title="@string/item_1"/>
                <item android:id="@+id/checkable_item_2"
                      android:title="@string/item_2"
                      android:checked="true"/>
                <item android:id="@+id/checkable_item_3"
                      android:title="@string/item_3"
                      android:checked="true"/>
            </group>
        </menu>
    </item>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值