启动ActionMode

时间:2016-06-04 11:47:04

标签: android toolbar android-actionmode

我有以下活动布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    style="@style/DrawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".ui.activity.MyActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:background="@color/black"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:collapsedTitleTextAppearance="@style/ToolbarTitle"
                app:contentScrim="@color/primary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_top"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:theme="@style/ThemeOverlay.AppCompat.Light"/>

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <my.package.ui.widget.MyHeaderWidget
            android:id="@+id/deal_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="my.package.ui.activity.MyHeaderBehavior"/>

        <include layout="@layout/content_list"/>
    </android.support.design.widget.CoordinatorLayout>

    <include layout="@layout/navigation_view"/>

</android.support.v4.widget.DrawerLayout>

它用于具有折叠工具栏+导航抽屉+标题动画和子标题(在标题小部件中)。 在我尝试启动ActionMode以启用多选之前,一切都很好。

我是通过致电:

来做到的
    mActionMode = mCollapsingToolbarLayout.startActionMode(mActionModeCallback);

问题是我结束了两个动作栏(带两个箭头):

See the screenshot

当我启动ActionMode时,黑色的那个是我期待的那个,白色的那个。

我做错了吗?

修改

AndroidMaifest

中的

    <activity
        android:name=".ui.activity.MyActivity"
        android:label="@string/title_activity"
        android:theme="@style/MyTheme">
    </activity>

styles.xml

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@drawable/window_background_gray</item>
    <item name="android:colorBackground">@color/app_background</item>
    <item name="displayOptions">showHome|homeAsUp|showTitle</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorAccent">@color/accent</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="android:textAppearanceButton">@style/Theme.ButtonTextAppearance</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

EDIT2

如果您想测试此问题,可以使用ScrollingActivity示例代码在Android studio中创建一个新项目。 按照您的意愿修改主题,然后在按FAB时启动操作模式,将以下对象作为ActionMode.Callback传递给startSupportActionMode:

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.menu_scrolling, 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.action_settings:
                mode.finish();
                return true;
            default:
                return false;
        }
    }

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

4 个答案:

答案 0 :(得分:16)

如果您正在使用工具栏,则这3行就足够了:

 <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">true</item> <!-- This is the important line -->
</style>

请记住在文件样式和样式中添加它(v21)

答案 1 :(得分:6)

您需要告诉系统您使用的是自定义工具栏。 为此,请在活动的onCreate中添加以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.your_layout);
  //Then get a reference to your Toolbar
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
  //Tell the system to use yours
  setSupportActionBar(toolbar);
}

此处我的活动扩展了AppCompatActivity,这就是我使用setSupportActionBar的原因,但您会发现常规活动setActionBar的相同方法

此外,您还需要创建样式(在res / values / styles.xml下):

<style name="MyNoAppBarTheme" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

或者如果您还没有覆盖AppTheme:

<style name="MyNoAppBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

最后在您的清单中为该特定活动设置此主题:

<activity
        android:name=".ui.activities.YourActivity"
        android:label="@string/app_name"
        android:theme="@style/MyNoAppBarTheme"
        />

编辑: 编辑完问题后,似乎确实是actionMode就是问题所在。正如this answer所述,请确保您使用正确的导入,并使用startSupportActionMode代替startActionMode

答案 2 :(得分:3)

尝试在styles.xml中添加以下主题 -

<item name="android:windowActionBarOverlay">false</item>
<item name="windowActionBarOverlay">false</item>
<item name="windowActionModeOverlay">true</item>

如果您使用的是Toolbar -

,请添加以下行
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

答案 3 :(得分:2)

您的父主题必须是NoAction栏主题的一些

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="colorControlNormal">@color/colorPrimary</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>