如何更改图标导航抽屉关闭或打开

时间:2014-12-21 07:41:28

标签: android

我的问题非常明确,我想在NavigationDrawer打开和关闭时更改Up图标。所以请告诉我如何更改动作栏中的upp图标。我已经尝试了很多,但我无法做到。 ii使用自定义布局来设置标题。提前致谢

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.center_action_bar_text);
        actionBar.setTitle("dvds");
        View view = actionBar.getCustomView();
        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText("zfdgfdg");
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);


        ---------------------------------
        drawerListView.setAdapter(drawerListAdapter);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


        drawerToggle = new ActionBarDrawerToggle(HomeActivity.this,
                drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, R.string.drawer_open,
                R.string.drawer_close){

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                    }

                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                    }
        };
        drawerLayout.setDrawerListener(drawerToggle);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
             return true;
           }
        return super.onOptionsItemSelected(item);
    }

I want to use this image when drawer open I want to use this image when drawer closed

2 个答案:

答案 0 :(得分:1)

将此方法添加到您的代码中:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

答案 1 :(得分:1)

我有其他一个答案

只需添加此方法: -

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider);
        //menu.findItem(R.id.sliding).setVisible(!drawerOpen);
        if(drawerOpen)
        {
            menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
        }
        else {
            menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon);
        }
        //menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher);
        //menu.findItem(R.id.action_settings).setVisible(!drawerOpen1);
        return super.onPrepareOptionsMenu(menu);
    }

对于该方法调用,您需要使用此tow方法

invalidateOptionsMenu();
// calling onPrepareOptionsMenu() to hide action bar icons
菜单打开时

    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(mDrawerTitle);
        //mDrawerLayout.openDrawer(Gravity.END);
        // calling onPrepareOptionsMenu() to hide action bar icons
        invalidateOptionsMenu();
    }
菜单关闭时

public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                //menu.findItem(R.id.sliding).;
                //mDrawerLayout.closeDrawer(Gravity.END);
                // calling onPrepareOptionsMenu() to show action bar icons

                invalidateOptionsMenu();
            }