我想要做的是在EditText的上下文菜单中添加一个项目。发生的事情是上下文菜单显示在常规操作栏上方。我希望它暂时替换常规操作栏。
Here is an image of the oddness that I am getting.
以下是与操作菜单和操作栏相关的代码。
public boolean onPrepareOptionsMenu(Menu menu) {
this.optionsMenu = menu;
menu.clear();
MenuInflater inflater = getMenuInflater();
switch (currentState) {
case ACTIVITY_STATE_EDIT:
inflater.inflate(R.menu.menu_edit_quote, menu);
break;
case ACTIVITY_STATE_NEW:
inflater.inflate(R.menu.menu_edit_quote, menu);
break;
}
updateSaveButtonState();
return true;
}
//
// Implementation of ActionMode.Callback interface
//
MenuItem addSetKeyWordMenuItem(Menu menu) {
MenuItem newItem = menu.add(0, ACTION_SET_KEYWORD, 0, "Set Keyword");
newItem.setIcon(R.drawable.ic_vpn_key_white_48dp);
newItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_ALWAYS);
return newItem;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
addSetKeyWordMenuItem(menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// addSetKeyWordMenuItem(menu);
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == ACTION_SET_KEYWORD) {
String newKey = quoteEditText.getText().subSequence(quoteEditText.getSelectionStart(), quoteEditText.getSelectionEnd()).toString();
keyEditText.setText(newKey);
quoteEditText.clearFocus();
updateSaveButtonState();
return true;
}
return false;
}
private void updateSaveButtonState() {
// Prevent the user from saving when there is no keyword set.
if (currentState == ACTIVITY_STATE_EDIT || currentState == ACTIVITY_STATE_NEW) {
if (this.optionsMenu != null) {
MenuItem saveItem = this.optionsMenu.findItem(R.id.menu_save);
if (saveItem != null) {
boolean state = !quoteEditText.getText().toString().isEmpty();
saveItem.setEnabled(state);
}
}
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
答案 0 :(得分:0)
在主题中设置actionMode overlay,如下所示:
<item name="windowActionModeOverlay">true</item>