Android 在工具栏上显示 ActionMode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26483778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:35:00  来源:igfitidea点击:

Display ActionMode over Toolbar

androidandroid-support-libraryandroid-toolbarandroid-actionmode

提问by Ga?tan

I am trying to use the android.view.ActionModewith the new android.support.v7.widget.Toolbar, in addition to the traditional android.app.ActionBar. I am able to display it with:

我试图用android.view.ActionModeandroid.support.v7.widget.Toolbar,除了传统的android.app.ActionBar。我能够显示它:

toolbar.startActionMode(callback);

The problem is that the ActionModeis displayed over the ActionBar, and not over the Toolbar. Is there a way to change that?

问题是ActionMode显示在 之上ActionBar,而不是显示在之上Toolbar。有没有办法改变它?

I tryied to set the following in my theme, but it does not seem to change anything:

我尝试在我的主题中设置以下内容,但它似乎没有改变任何东西:

<item name="windowActionModeOverlay">true</item>

回答by Kuffs

Since you are using the Toolbar, I also assume you are using the AppCompatActivityand have replaced the built in ActionBarwith your custom Toolbarusing setSupportActionBar(toolbar);

由于您使用的是Toolbar,我还假设您正在使用 AppCompatActivity并且已将内置替换为ActionBar您的自定义Toolbar使用 setSupportActionBar(toolbar);

First of all ensure you are importing the correct namespace:

首先确保您导入了正确的命名空间:

import androidx.appcompat.view.ActionMode;
// Or
import android.support.v7.view.ActionMode;

and NOT

并不是

import android.view.ActionMode;

then use

然后使用

_actionMode = startSupportActionMode(this);

and NOT

并不是

_actionMode = startActionMode(this);

回答by Frank

Do not start it on your activity, but on your toolbar. In you activity:

不要在您的活动中启动它,而是在您的工具栏上启动它。在你的活动中:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.startActionMode(mActionModeCallback)

and you have to use

你必须使用

<item name="windowActionModeOverlay">true</item>

in your theme as stated by Andre.

正如安德烈所说,在你的主题中。

回答by André Restivo

Try this in your theme:

在你的主题中试试这个:

<item name="windowActionModeOverlay">true</item>

回答by Lefty

I think the one thing people are not making clear is that the line

我认为人们没有说清楚的一件事是这条线

<item name="windowActionModeOverlay">true</item>

is placed in the Base theme i.e AppTheme and not the AppTheme.NoActionBar

放置在基本主题中,即 AppTheme 而不是 AppTheme.NoActionBar

<resources>

<!-- Base application theme. -->
<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="windowActionModeOverlay">true</item>
</style>

<style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsTranslucent">true</item>
</style>


<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

回答by zhangjin

find your AndroidManifest.xml ,next add below code in your application or Activity theme

找到您的 AndroidManifest.xml ,然后在您的应用程序或活动主题中添加以下代码

<item name="windowActionModeOverlay">true</item>

so like:

像这样:

<style name="WorkTimeListTheme" parent="AppTheme.NoActionBar">
        <item name="windowActionModeOverlay">true</item>
        <item name="actionModeBackground">@color/theme_primary</item>
    </style>

回答by Avery

This is the solution I made.

这是我提出的解决方案。

In my onCreateActionModemethod of ActionMode.Callback, I add this:

ActionMode.Callback 的onCreateActionMode方法中,我添加了以下内容:

StandaloneActionMode standaloneActionMode = (StandaloneActionMode) actionMode;
Field mContextView;
try {
     mContextView = StandaloneActionMode.class.getDeclaredField("mContextView");
     mContextView.setAccessible(true);
     View contextView = (View) mContextView.get(standaloneActionMode);
     MarginLayoutParams params = (MarginLayoutParams) contextView.getLayoutParams();
     params.topMargin = mToolbar.getTop();
  } catch (NoSuchFieldException e) {
            e.printStackTrace();
  } catch (IllegalAccessException e) {
            e.printStackTrace();
  } catch (IllegalArgumentException e) {
            e.printStackTrace();
  }

It works for me.

这个对我有用。

回答by YanQing

I have tried all the methods above, but it still doesn`t work. And then, I tried the below method:

上面的方法我都试过了,还是不行。然后,我尝试了以下方法:

    private class ActionModeCallback implements ActionMode.Callback {
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        actionMode.getMenuInflater().inflate(R.menu.note_find_action, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().show();
    }
}

Here, I used action mode and startSupportActionMode method of support library. At the same time I have also tried to modify the theme of given activity. Surely, it doesn`t work. So, if you really have no better choice you may try this one.

在这里,我使用了支持库的动作模式和 startSupportActionMode 方法。同时我也尝试修改给定活动的主题。当然,它不起作用。所以,如果你真的没有更好的选择,你可以试试这个。

Just recently, I have found that I used the Colorful frame to enable multiple theme of my app, this will change the theme in code. When I tried to modify the style in this framework, it works.

就在最近,我发现我使用五颜六色的框架来启用我的应用程序的多个主题,这将更改代码中的主题。当我尝试修改此框架中的样式时,它起作用了。

Hope it works.

希望它有效。

回答by user2416658

If you see the view tree,You will can write below code:

如果您看到视图树,您将可以编写以下代码:

 ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
    traverseView(decorView);
 /**
 * traverse find actionmodebar
 * @param root  view
 */
public void traverseView(View root) {
    if (root==null){
        return;
    }
    if (root instanceof ActionBarContextView){
        root.setVisibility(View.GONE);
        return;
    }
    if ((root instanceof ViewGroup)) { // If view is ViewGroup, apply this method on it's child views
        ViewGroup viewGroup = (ViewGroup) root;
        for (int i = 0; i < viewGroup.getChildCount(); ++i) {
            traverseView(viewGroup.getChildAt(i));
        }
    }
}