Display ActionMode over Toolbar

后端 未结 9 1668
臣服心动
臣服心动 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:56

    So, after days going thru this thread, I finally got it working. I'd like to summarize what I did.

    Note: This is the solution using a Toolbar to replace the default ActionBar in AppCompatActivity.

    1. I added this line to my AppTheme: It tells android that you want your action mode to overlay the toolbar
    <item name="windowActionModeOverlay">true</item>
    
    1. Use the right imports:

    You have to use these imports in order to work with AppCompatActivity:

    import androidx.appcompat.view.ActionMode;
    // or
    import android.support.v7.view.ActionMode;
    
    1. Start the ActionMode on your Activity like so:
    actionMode = startSupportActionMode(callback);
    

    And not like so:

    actionMode = startActionMode(callback);
    

    You Activity creates the ActionMode on the toolbar automatically, because it's set as the supportActionToolbar. The style handles the dsiplaying as overlay.

    Thanks to @Kuffs and @Lefty.

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

    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));
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:01

    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.

    0 讨论(0)
提交回复
热议问题