问题
I'm trying to remove default items, such as "Copy" and "Select All", from the CAB when the user selects some text from a TextView element.
I've overridden the default action using this code, but inflating a new menu resource doesn't remove default items.
textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Test");
mode.getMenuInflater().inflate(R.menu.new_menu_selection, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
Is there a solution to this?
回答1:
To remove the default items you can call menu.clear()
before inflate the new menu:
textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
mode.setTitle("Test");
mode.getMenuInflater().inflate(R.menu.new_menu_selection, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
来源:https://stackoverflow.com/questions/33597729/remove-default-cab-items-in-textview-selection-actionmode