Android 设置为始终显示的菜单项未显示

Android menu item set to always be shown is not being shown

对 Android 中的菜单项使用 app:showAsAction="always" 不会显示 Action Bar 中的菜单项。相反,该项目与具有 app:showAsAction="never" 的菜单项一起显示在下拉列表中。以下是代码:

menu_main.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="myAppID.MainActivity" >
<item android:id="@+id/action_one"
    android:title="One"
    app:showAsAction="always" />
<item android:id="@+id/action_two"
    android:title="Two"
    app:showAsAction="never" />
<item android:id="@+id/action_three"
    android:title="Three"
    app:showAsAction="never" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

附加信息:我没有使用 AppCompat。我注意到当使用 AppCompat 时,菜单项的行为是正确的。

没有 AppCompat 我该如何解决这个问题?

而不是 app:showAsAction 将其更改为 android:showAsAction,如图所示:

<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="myAppID.MainActivity" >
      <item android:id="@+id/action_one"
          android:title="One"
          android:icon="@drawable/your_icon"
          android:showAsAction="always" />
      <item android:id="@+id/action_two"
          android:title="Two"
          android:showAsAction="never" />
      <item android:id="@+id/action_three"
          android:title="Three"
          android:showAsAction="never" />
  </menu>

希望有用。

你可以用这个方法

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}