Display ActionMode over Toolbar

后端 未结 9 1630
臣服心动
臣服心动 2020-12-02 09:59

I am trying to use the android.view.ActionMode with the new android.support.v7.widget.Toolbar, in addition to the traditional android.app.Act

相关标签:
9条回答
  • 2020-12-02 10:38

    Since you are using the Toolbar, I also assume you are using the AppCompatActivity and have replaced the built in ActionBar with your custom Toolbar using 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);
    
    0 讨论(0)
  • 2020-12-02 10:40

    Try this in your theme:

    <item name="windowActionModeOverlay">true</item>
    
    0 讨论(0)
  • 2020-12-02 10:40

    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

    <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" />
    

    0 讨论(0)
  • 2020-12-02 10:45

    This is the solution I made.

    In my onCreateActionMode method of ActionMode.Callback, I add this:

    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.

    0 讨论(0)
  • 2020-12-02 10:54

    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.

    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.

    0 讨论(0)
  • 2020-12-02 10:55

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

    <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>
    
    0 讨论(0)
提交回复
热议问题