Android导航栏不显示汉堡包



为什么我的导航抽屉在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>

为什么图标汉堡包不显示?

还要记住,ActionBarActivity已弃用使用:AppCompatActivity和关于NavigationDrawer你应该使用NavigationView

看起来您正在使用支持库v4中发现的已弃用的ActionBarDrawerToggle版本。您的IDE应该警告您这种用法。您还在使用已弃用的ActionBarActivity .

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

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

相关内容

  • 没有找到相关文章

最新更新