在它们之间切换时如何防止actionMode和工具栏“跳转”?

问题描述 投票:2回答:2

背景

我在我的应用程序中有一个活动,该活动具有一个工具栏作为actionBar,并且还具有一个actionMode,用于项目的多选。

问题

每次关闭actionMode时,两种模式之间都会出现“跳转”,因此我可以同时看到工具栏和actionMode。

也许我只是做错了,但我记得过去做得很好。

这是使用我制作的代码段的外观:

enter image description here

我尝试过的

这是我使用的代码的片段。要对其进行测试,请运行该应用程序,稍等片刻以使actionMode出现,然后按返回按钮或按actionMode上的按钮。请注意,我使用的所有类都属于支持库(如果有)。

MainActivity.java

public class MainActivity extends AppCompatActivity {
    protected ActionMode.Callback _actionModeCallback;
    protected ActionMode _actionMode;
    Toolbar _toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _toolbar = (Toolbar) findViewById(R.id.activity_app_list__toolbar);
        setSupportActionBar(_toolbar);
        _actionModeCallback = new ActionMode.Callback() {
            @Override
            public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(final ActionMode mode) {
                _toolbar.setVisibility(View.VISIBLE);
                _actionMode = null;
            }

            @Override
            public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
                _toolbar.setVisibility(View.GONE);
                return true;
            }

            @Override
            public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
                mode.finish();
                return true;
            }
        };
        //
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                _actionMode = startSupportActionMode(_actionModeCallback);
            }
        }, 2000);
    }

}

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_app_list__toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:colorControlNormal="?attr/colorControlNormal"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        tools:ignore="UnusedAttribute"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some Text"/>
    </FrameLayout>
</LinearLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->.
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        <item name="colorPrimary">#FF0288D1</item>
    </style>

</resources>

问题

为什么会这样?我该如何解决?

这是一个已知的错误吗?

android android-actionbar android-toolbar android-actionmode
2个回答
2
投票

您正在寻找的是ACTION_MODE_OVERLAY标志。在您的Activity.onCreate()方法中,在对super.onCreate()的调用之前添加以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
    super.onCreate(savedInstanceState);
    // other stuff...
}

0
投票

也可以通过在<item name="windowActionModeOverlay">true</item>中添加AppTheme来实现与@Kevin Coppock所述的相同。

您还可以添加<item name="actionModeStyle">@style/ActionModeTheme</item>以进一步修改相应样式的动作模式。

主题可能看起来像这样:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->.
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    <item name="colorPrimary">#FF0288D1</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeStyle">@style/ActionModeTheme</item>
</style>

<style name="ActionModeTheme" parent="AppTheme">
    <item name="background">@color/colorOfYourChoice</item>
    <item name="titleTextStyle">@style/TextStyleOfYourChoice</item>
    <-- there are even more properties available to customize -->
</style>

© www.soinside.com 2019 - 2024. All rights reserved.