Display ActionMode over Toolbar

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

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

toolbar.startActionMode(callback); 

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

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

true

回答1:

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 android.support.v7.view.ActionMode; 

and NOT

import android.view.ActionMode; 

then use

_actionMode = startSupportActionMode(this); 

and NOT

_actionMode = startActionMode(this); 


回答2:

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

true

in your theme as stated by Andre.



回答3:

Try this in your theme:

true


回答4:

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.



回答5:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!