ActionBar上的Android导航抽屉图标

时间:2015-01-07 16:10:34

标签: android android-actionbar icons navigation-drawer drawertoggle

public class MainActivity extends ActionBarActivity {
    private String[] drawerListItems;
    private ListView drawerListView;
    private DrawerLayout drawerlayout;
    private ActionBarDrawerToggle drawToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          

        drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerListItems = getResources().getStringArray(R.array.DrawreMenue);
        drawerListView = (ListView) findViewById(R.id.slidermenu);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        //getActionBar().setIcon(R.drawable.ic_launcher);
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, drawerListItems));
       //drawerListView.setOnItemClickListener(this);
       drawerListView.setOnItemClickListener(new DrawerItemClickListener());

        drawToggle = new ActionBarDrawerToggle(this, drawerlayout,toolbar,  
                R.string.draw_open,  
                R.string.draw_close 
        ) {


            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);

            }    

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
        } ;
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);

       getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_drawer);

        drawerlayout.setDrawerListener(drawToggle);
        drawToggle.syncState();
    }

我在Android Studio中使用此代码

minSdkVersion 8

targetSdkVersion 21

显示抽屉图标,但点击它不打开抽屉列表而不是在滑动打开时关闭它

2 个答案:

答案 0 :(得分:1)

您需要再添加两个方法onPostCreateonConfigurationChanged

mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

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

Source

答案 1 :(得分:0)

首先,您需要找到要单击以打开抽屉的按钮的ID,然后将代码放入点击侦听器中以打开

mDrawerLayout.openDrawer(mDrawer);

并关闭抽屉

mDrawerLayout.closeDrawer(mDrawer);