0
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でこのコードを使用しています

minSdkバージョン 8

targetSdkバージョン 21

引き出しアイコンを表示していますが、クリックすると引き出しリストが開かず、スライドして開いたときに閉じません

4

2 に答える 2

1

onPostCreateさらに 2 つのメソッドを追加する必要があります。onConfigurationChanged

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);
    }

ソース

于 2015-01-09T05:51:18.097 に答える
0

まず、クリックしてドロワーを開くボタンのIDを見つける必要があります。次に、クリックリスナーにコードを入力して開きます

mDrawerLayout.openDrawer(mDrawer);

引き出しを閉めて入れます

mDrawerLayout.closeDrawer(mDrawer);
于 2015-01-07T18:14:14.553 に答える