操作栏的菜单按钮仅在操作 Over flow 中显示

The menu button of the action bar are being displayed only in the action Over flow

所以我使用的是 appCombat 操作栏,操作栏中显示了操作菜单按钮,但我迁移到了全息主题和主题 material。现在,操作菜单按钮仅显示在操作溢出(三个点)中。是否可以删除操作溢出并且操作菜单按钮将仅显示在操作栏中?

来自 menu.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">
  <!-- search -->
  <item android:id="@+id/action_search"
        android:icon="@drawable/ic_search"
        android:title="search"
        android:visible="false"
        app:showAsAction="always"
      />
<!-- share -->
  <item android:id="@+id/action_share"
      android:icon="@drawable/ic_share"
      android:title="share"
      app:showAsAction="ifRoom"
      android:visible="false"/>
</menu>

主要代码 activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // toggle nav drawer on selecting action bar app icon/title
    if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    menu.findItem(R.id.action_search).setVisible(false);
    menu.findItem(R.id.action_share).setVisible(false);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mExpandableListView);

    if(mDrawerLayout!=null && drawerOpen)
        menu.clear();

    return super.onPrepareOptionsMenu(menu);
}

来自与主 activity 相关的片段的代码:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    //the button search is setting to visible
    menu.findItem(R.id.action_search).setVisible(true);
    menu.findItem(R.id.action_share).setVisible(false);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_search:
                isSearchMode = true;
                //Log.d("billy","inside action_search");
                rightHeaderButtonClick();
                return true;
        case R.id.action_share:
            return false;
        default:
            return super.onOptionsItemSelected(item);
    }
}

第二个代码 activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    menu.findItem(R.id.action_search).setVisible(false);
    menu.findItem(R.id.action_share).setVisible(true);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_search:
            return false;
        case R.id.action_share:
            doShare();
        case android.R.id.home:
            ActivityDetails.this.finish();
        ActivityDetails.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return true;
        default:
            return super.onOptionsItemSelected(item);

    }
}

我所有的活动都扩展到 Activity, 谢谢!!

Now the action menu buttons are being displayed only in the action overflow (three dots)

那是因为您仍在菜单 XML 中使用 app: 命名空间。这仅适用于 appcompat-v7 或其他图书馆。对于本机操作栏,将 app:showAsAction 更改为 android:showAsAction