操作栏菜单图标在onDrawerOpened中消失

时间:2016-04-07 04:53:54

标签: android android-actionbar

我是Android的新手。我在菜单中添加了菜单图标。 这是我的代码。

<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=".MainActivity">

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="1"
        android:showAsAction="never" />

    <item
        android:id="@+id/action_example"
        android:title="Logout"
        android:orderInCategory="2"
        android:showAsAction="withText|ifRoom" />

    <item
        android:id="@+id/addLoc"
        android:title=""
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_location"
        android:onClick="gotoLocation"/>

    <item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>

    <item
        android:id="@+id/profPicture"
        android:title=""
        android:orderInCategory="3"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_prof"
        android:onClick="userProfile"
        />

    <item
        android:id="@+id/namePerson"
        android:title="Nathalia Smith"
        android:orderInCategory="4"
        app:showAsAction="ifRoom"
        android:onClick="userProfile"/>
</menu>

将这些菜单图标添加到我的操作栏菜单图标后出现。但是,当我展开导航抽屉时,图标就会消失,当我关闭时,图标会出现。

我需要的是在抽屉打开时出现这些图标。

这是我的onCreate。

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle(); //

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFA500"))); // change the color of header

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
        {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();          }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getSupportActionBar().setIcon(R.drawable.ic_deals);
    }

这是我的onDrawerClosed和onDrawerOpened

  mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
        {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();          }
        };
我需要的是,只要打开抽屉,就会出现这些图标。有人请帮忙。

提前致谢。 :)

2 个答案:

答案 0 :(得分:1)

从以下代码中移除invalidateOptionsMenu();

       mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
                {
                    public void onDrawerClosed(View view) {
                        getSupportActionBar().setTitle(mTitle);

                    }
                    public void onDrawerOpened(View drawerView) {

                       getSupportActionBar().setTitle(mDrawerTitle);

                   }
                };

此外,如果您想要始终显示图标,请使用app:showAsAction="always",如下所示:

<item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="always"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>

答案 1 :(得分:0)

invalidateOptionsMenu()用于说Android,菜单内容已更改,菜单应重新绘制。例如,单击在运行时添加另一个菜单项的按钮,或隐藏菜单项组。在这种情况下,您应该调用invalidateOptionsMenu(),以便系统可以在UI上重绘它。此方法是OS调用onPrepareOptionsMenu()的信号,您可以在其中实现必要的菜单操作。此外,OnCreateOptionsMenu()在活动(片段)创建期间仅被调用一次,因此此方法无法处理运行时菜单更改。

以上信息来源:

http://stackoverflow.com/questions/27984041/android-correct-use-of-invalidateoptionsmenu

相关问题