操作栏的菜单按钮仅显示在动作过流

时间:2015-07-16 17:27:31

标签: android android-actionbar android-menu

所以我正在使用appCombat操作栏,操作栏中显示了操作菜单按钮,但我对主题全息和主题素材进行了移植。现在,动作菜单按钮仅在动作溢出(三个点)中显示。是否可以删除操作溢出,操作菜单按钮只会显示在操作栏中?

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>

主要活动的代码:

@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);
}

来自与主要活动相关联的片段的代码:

@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);
    }
}

来自第二项活动的代码:

@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, 谢谢!!

1 个答案:

答案 0 :(得分:1)

  

现在,动作菜单按钮仅显示在动作溢出(三个点)

这是因为您仍然在菜单XML中使用app:命名空间。这仅适用于appcompat-v7或其他库。对于原生操作栏,请将app:showAsAction更改为android:showAsAction