【问题标题】:The menu button of the action bar are being displayed only in the action Over flow操作栏的菜单按钮仅在溢出操作中显示
【发布时间】:2015-07-17 01:33:27
【问题描述】:

所以我使用了 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 , 谢谢!!

【问题讨论】:

    标签: android android-actionbar android-menu


    【解决方案1】:

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

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

    【讨论】:

    • 现在标签项是红色的,我有一个提示说菜单项应该指定一个标题,但我已经写了一个标题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多