我在 xml 文件中尝试了不同的组合:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_create_alarm"
android:icon="@drawable/ic_action_accept"
android:orderInCategory="100"
android:title="@string/menu_create_alarm"
app:showAsAction="ifRoom|withText" />
</menu>
或者
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_create_alarm"
android:icon="@drawable/ic_action_accept"
android:orderInCategory="100"
android:title="@string/menu_create_alarm"
app:showAsAction="always|withText" />
</menu>
或者
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_create_alarm"
android:icon="@drawable/ic_action_accept"
android:orderInCategory="100"
android:title="@string/menu_create_alarm"
app:showAsAction="withText" />
</menu>
或者
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_create_alarm"
android:icon="@drawable/ic_action_accept"
android:orderInCategory="100"
android:title="@string/menu_create_alarm"
android:showAsAction="always|withText" />
</menu>
我试图以编程方式设置它
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
MenuItem item = menu.add(R.string.menu_create_alarm);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT|MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setIcon(R.drawable.ic_action_accept);
item.setOnMenuItemClickListener(
new OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
saveAlarm();
return true;
}
}
);
// inflater.inflate(R.menu.menu_create_alarm, menu);
super.onCreateOptionsMenu(menu, inflater);
}
或者
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_create_alarm"
android:icon="@drawable/ic_action_accept"
android:orderInCategory="100"
android:title="@string/menu_create_alarm"
android:showAsAction="always|withText"
app:showAsAction="always|withText" />
</menu>
但是,仅显示图标。并且有空间,因为我没有设置工具栏标题。删除菜单并用工具栏中的按钮替换它们是不合适的。
如何显示文字?