Android 导航抽屉不显示汉堡包

Android Navigation drawer not displaying hamburger

为什么 android 中的导航抽屉不显示汉堡包图标。 该做的我都做了还是没有显示出来?

            public class Main extends ActionBarActivity {

            private String[] mPlanetTitles;
            private DrawerLayout mDrawerLayout;
            private ListView mDrawerList;
            private CharSequence mTitle;
            private ActionBarDrawerToggle mDrawerToggle;

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

                mTitle = "test";

                ArrayList<Integer> draw=new ArrayList();
                draw.add(R.drawable.elec);draw.add(R.drawable.mob);draw.add(R.drawable.auto);draw.add(R.drawable.pet);draw.add(R.drawable.est);

                mPlanetTitles = new String[]{"Electronic ads", "Mobile ads", "Automobile ads","Pets ads","Real Estate ads"};
                mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                mDrawerList = (ListView) findViewById(R.id.left_drawer);
                Adapt c=new Adapt(this,draw);
                mDrawerList.setAdapter(c);

                DisplayMetrics displayMetrics =getApplicationContext().getResources().getDisplayMetrics();
                int height=(int)((300 * displayMetrics.density) + 0.5);


                GridView g=(GridView)findViewById(R.id.gridview);


                ImageAdapter a=new ImageAdapter(this,draw);
                g.setAdapter(a);
                g.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                     @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        startActivity(new Intent(Main.this, MainActivity.class));
                    }
                });


                // Set the adapter for the list view
            //    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                       // R.layout.drawer_list_item, mPlanetTitles));
                // Set the list's click listener
                mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        mDrawerList.setItemChecked(i, true);
                        //setTitle(mPlanetTitles[position]);
                        mDrawerLayout.closeDrawer(mDrawerList);
                    }
                });

                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) {
                        mDrawerToggle.syncState();
                        getSupportActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();
                    }

                    /** Called when a drawer has settled in a completely open state. */
                    public void onDrawerOpened(View drawerView) {
                        mDrawerToggle.syncState();
                        getSupportActionBar().setTitle(mTitle);
                        mDrawerList.bringToFront();
                        invalidateOptionsMenu();
                    }

                };

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

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


            }





            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            protected void onPostCreate(Bundle savedInstanceState) {

                super.onPostCreate(savedInstanceState);
                mDrawerToggle.syncState();

                // Sync the toggle state after onRestoreInstanceState has occurred.

            }





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

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Pass the event to ActionBarDrawerToggle, if it returns
                // true, then it has handled the app icon touch event
                if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
                }
                // Handle your other action bar items...

                return super.onOptionsItemSelected(item);
            }

            /**
             * Swaps fragments in the main content view
             */
            private void selectItem(int position) {
                Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();

                // Highlight the selected item, update the title, and close the drawer
                mDrawerList.setItemChecked(position, true);
                setTitle(mPlanetTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);
            }




            @Override
            public void setTitle(CharSequence title) {
                mTitle = title;
                getSupportActionBar().setTitle(mTitle);
            }

            private class DrawerItemClickListener implements ListView.OnItemClickListener {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    selectItem(position);
                }
            }

        }

这是我的布局文件。

           <android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <!-- The main content view -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <GridView
                android:id="@+id/gridview"
                android:layout_width="match_parent"
                android:numColumns="3"
                android:gravity="center"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:layout_height="match_parent"/>
            </RelativeLayout>
            <!-- The navigation drawer -->
            <ListView
                android:id="@+id/left_drawer"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"

                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                />
        </android.support.v4.widget.DrawerLayout>

为什么图标汉堡没有显示?

您似乎在使用 ActionBarDrawerToggle found in v4 of the support library 的已弃用版本。您的 IDE 应该警告您这种用法。您还使用了已弃用的 ActionBarActivity.

您应该切换到 v7 支持库中的新 ActionBarDrawerToggle

更换较新的很容易 - 只需更改您的导入并删除 R.drawable.ic_drawer 参数即可。新的切换为您提供抽屉可绘制。

另请记住,ActionBarActivity 弃用 使用:AppCompatActivity 关于 NavigationDrawer 您应该使用 NavigationView