我在这里尝试了一些消化,但没有人工作,
这是问题,箭头表示我想要的地方
操作栏只显示2个图标
,另一个在3点菜单中
我的custom_menu.xml
<item
android:id="@+id/btnBold"
android:icon="@drawable/text_bold"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="Teste1">
</item>
<item
android:id="@+id/btnItalic"
android:icon="@drawable/text_italic"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="teste2">
</item>
<item
android:id="@+id/btnUnderline"
android:icon="@drawable/text_underline"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="teste3">
</item>
<item
android:id="@+id/btnStrike"
android:icon="@drawable/text_strike"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="teste2">
</item>
<item
android:id="@+id/btnForeground"
android:icon="@drawable/text_foreground"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="teste2">
</item>
<item
android:id="@+id/btnBackground"
android:icon="@drawable/text_background"
app:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="teste2">
</item>
</menu>
和MainActivity的代码
private ActionMode mActionMode = null;
@Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
// Remove the default menu items (select all, copy, paste, search)
menu.clear();
// If you want to keep any of the defaults,
// remove the items you don't want individually:
// menu.removeItem(android.R.id.[id_of_item_to_remove])
// Inflate your own menu items
mode.getMenuInflater().inflate(R.menu.my_custom_menu, menu);
}
super.onActionModeStarted(mode);
}
// This method is what you should set as your item's onClick
// <item android:onClick="onContextualMenuItemClicked" />
public void onContextualMenuItemClicked(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnBold:
// do some stuff
break;
case R.id.btnItalic:
// do some different stuff
break;
default:
// ...
break;
}
// This will likely always be true, but check it anyway, just in case
if (mActionMode != null) {
//mActionMode.finish();
}
}
@Override
public void onActionModeFinished(ActionMode mode) {
mActionMode = null;
super.onActionModeFinished(mode);
}
答案 0 :(得分:0)
你有没有尝试过这个问题的答案?
Android 4.3 menu item showAsAction="always" ignored
基本上,他说的是你必须缺少命名空间。 正确的方法是:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_add_size"
android:title="@string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="@android:drawable/ic_menu_add" />
</menu>
我遇到了这个问题,并通过更改特定的命名空间来解决它。希望你也可以通过这样做来解决。