【问题标题】:How to prevent the actionMode and toolbar to "jump" when toggling between them?如何防止 actionMode 和工具栏在它们之间切换时“跳转”?
【发布时间】:2015-09-14 22:52:12
【问题描述】:

背景

我的应用中有一个 Activity,它有一个作为 actionBar 的工具栏,它还有一个 actionMode,用于多选项目。

问题

每次关闭actionMode,两种模式之间都会有一个“跳转”,所以我可以同时看到toolbar和actionMode。

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

这是使用我制作的 sn-p 代码的样子:

我尝试过的

这是我使用的代码的 sn-p。要对其进行测试,请运行应用程序,等待 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


【解决方案1】:

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

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

【讨论】:

  • 这修复了工具栏的问题,但现在下面的内容变成了actionMode后面的内容。也许我应该将布局设置为 FrameLayout,并且工具栏下方的内容有边距而不是在它下方?另外,是否可以改为通过主题设置您编写的内容?我还注意到,在进入 actionMode 时,我会在短时间内看到窗口的背景。我应该放一个带有工具栏背景的视图吗?
  • 我认为如果在主题中使用“windowActionModeOverlay”可以做同样的事情。事实上,我在原始应用程序中使用它,但我不记得为什么我将其设置为 false 而不是将其设置为 true。也许这就是它直到最近的工作方式。
  • 无论如何,如果您对如何顺利过渡到 actionMode 有任何建议,请告诉我。目前,您的努力会获得 +1。 :)
【解决方案2】:

@Kevin Coppock 所描述的相同也可以通过在您的AppTheme 中添加&lt;item name="windowActionModeOverlay"&gt;true&lt;/item&gt; 来实现。

主题可能如下所示:

<!-- 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>
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2020-07-21
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多