如何在Actionbar中显示菜单图标?

时间:2015-12-10 09:36:09

标签: android android-actionbar navigation-drawer

我在主要活动中添加了导航抽屉。现在我想在操作栏标题旁边有一个菜单图标。所以我做了以下事情:

    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close)
    {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
        }
    };
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
    mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (dlDrawer.isDrawerVisible(GravityCompat.START)) {
                dlDrawer.closeDrawer(GravityCompat.START);
            } else {
                dlDrawer.openDrawer(GravityCompat.START);
            }
        }
    });
    mDrawerToggle.syncState();

我也有:

    getActionBar().setDisplayShowHomeEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

我无法使用getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);,因为我的最低API级别为16。

这是菜单的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="com.app.activities.MainActivity">
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="200" app:showAsAction="never" />
</menu>

但是,使用上面的代码,应用程序图标旁边没有显示菜单图标。点击应用程序图标根本没有任何反应。

我需要添加哪些代码才能显示菜单图标,并且活动会对点击应用图标做出反应?

修改

要让应用图标点击工作,我在onOptionsItemSelected中添加了以下代码:

    int id = item.getItemId();

    if (id == android.R.id.home) {
        if (dlDrawer.isDrawerVisible(GravityCompat.START)) {
            dlDrawer.closeDrawer(GravityCompat.START);
        } else {
            dlDrawer.openDrawer(GravityCompat.START);
        }
    }

但是,菜单图标仍未显示。

1 个答案:

答案 0 :(得分:0)

按以下方式更改您的代码

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ){
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

用于选项菜单

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /***
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }