我们如何在Android上下文操作栏中显示后退按钮而不是完成(选中标记)按钮

时间:2014-12-12 07:13:05

标签: android android-actionbar contextual-action-bar android-cab

我默认情况下会这样做 enter image description here

我想要这个

enter image description here

这应该是微不足道的,但我找不到与Android文档相关的任何内容。

private void setupContextualBar()
    {
        mActionModeCallback = new ActionMode.Callback()
        {
            // Called when the action mode is created; startActionMode() was called
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) 
            {
                MenuInflater inflater = getActivity().getMenuInflater();
                inflater.inflate(R.menu.my_menu , menu);
                mCABMenu = 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) 
            {
                updateContextualBar();
                return true;
            }

            // Called when the user selects a contextual menu item
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) 
            {
                int menuItemId = item.getItemId();
                boolean eventConsumed = false;
                switch (menuItemId)
                {
                     //handle cases here
                }
                if (eventConsumed)
                {
                     updateContextualBar();
                }
                return eventConsumed;
            }

            // Called when the user exits the action mode
            @Override
            public void onDestroyActionMode(ActionMode mode) 
            {
                mActionMode = null;
            }
        };

4 个答案:

答案 0 :(得分:10)

您可以使用与您的活动相关联的自定义主题更改此按钮图像,如下所示:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="android:actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>

的AndroidManifest.xml:

 <activity
        android:name="MyActivity"
        android:theme="@style/MyCustomTheme"
  ...

我已经研究过但发现没有办法以编程方式更改action_mode_close_button(有些黑客尝试这样做,但它们有严重的潜在副作用)。似乎唯一可靠/安全的方式来改变这个图像是通过主题改变。

答案 1 :(得分:3)

如果您使用的是默认操作模式:startActionMode(...)

您可以使用:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="android:actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>

但是如果您使用的是Android支持V7操作模式:startSupportActionMode(...)

您应该使用:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>

答案 2 :(得分:0)

试试这个:

ActionBar ab;
    ab = getActionBar();
    ab.setHomeButtonEnabled(true);
    ab.setTitle("Back");
    ab.setIcon(R.drawable.iconBack);

并覆盖函数onOptionsItemSelected:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if(item.getItemId() == android.R.id.home)
            finish();
        return super.onOptionsItemSelected(item);
    }

希望它有所帮助!

答案 3 :(得分:0)

这个问题有点老了,但是我决定做出一个答案,因为我今天遇到了同样的问题,并且设置活动主题对于我的用例而言并不是令人满意的答案。如果每个活动仅需要一个特定的关闭图标,则最好在该活动的主题中设置actionModeCloseDrawable。但是,如果您需要在活动中为操作模式使用多个关闭图标,则必须使用反射更改关闭ImageView的drawable:

/**
 * Attempts to change the ActionMode close icon drawable using reflection.
 *
 * @param drawableResId
 *      Resource ID of the new drawable
 *
 * @return true if the drawable was changed, false otherwise.
 */
private boolean setActionCloseDrawable(@Nullable final ActionMode actionMode, 
                                       final int drawableResId)
{
    if (actionMode != null)
    {
        // Change the drawable for the close ImageView to the desired icon
        // Unfortunately, this can only be done with reflection
        try
        {
            final Field wrappedObjectField = actionMode.getClass().getDeclaredField("mWrappedObject");
            wrappedObjectField.setAccessible(true);
            final Object mWrappedObject = wrappedObjectField.get(actionMode);

            final Field contextViewField = mWrappedObject.getClass().getDeclaredField("mContextView");
            contextViewField.setAccessible(true);
            final Object mContextView = contextViewField.get(mWrappedObject);

            final Field closeField = mContextView.getClass().getDeclaredField("mClose");
            closeField.setAccessible(true);
            final Object mClose = closeField.get(mContextView);

            if (mClose instanceof ImageView)
            {
                ((ImageView) mClose).setImageDrawable(getResources().getDrawable(drawableResId));
                return true;
            }
        }
        catch (Exception ex)
        {
            Log.e(TAG, ex.getClass().getSimpleName() + " in #setActionCloseDrawable: " +
                    ex.getLocalizedMessage() + '\n' + Log.getStackTraceString(ex));
        }
    }

    return false;
}