工具栏导航按钮

ToolBar navigation button

这是我的工具栏:

这是 Gmail 应用工具栏:

我想要 Gmail 应用程序具有的打开导航抽屉的按钮。

我怎样才能做到这一点?

这是我的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:background="#EDAA00" />

这是我的应用主题:

<style name="noActionBar" parent="Theme.AppCompat.Light.NoActionBar"/>

这是我创建工具栏的代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

...

 mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        )

提前致谢!

在 onCreate 中使用这样的代码:

toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);// Supported toolbar
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); //this is the navigation drawer
content_frame=(FrameLayout)findViewById(R.id.content_frame);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open,R.string.drawer_close){
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float moveFactor = (getActivity().findViewById(R.id.left_drawer).getWidth() * slideOffset);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                {
                    content_frame.setTranslationX(moveFactor);
                }
                else
                {
                    TranslateAnimation anim = new TranslateAnimation(lastTranslate, moveFactor, 0.0f, 0.0f);
                    anim.setDuration(0);
                    anim.setFillAfter(true);
                    content_frame.startAnimation(anim);
                    lastTranslate = moveFactor;
                }
                super.onDrawerSlide(drawerView, slideOffset);
            }
        };
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                actionBarDrawerToggle.syncState();
            }
        });

 @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(drawerListview);
        } else super.onBackPressed();
    }

但不要忘记 v7 支持库:

import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;

不向工具栏添加任何可绘制对象 ActionBarDrawerToggle v7 添加它并赋予它动画,当抽屉打开时它转换为后退按钮

也看到这个linkDrawer Arrow Drawable

您可以使用以下样式:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

请参考这个link

我添加到这张照片的可绘制新ic_drawer

您正在为 ActionBarDrawerToggle 使用已弃用的构造函数。

 mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),                    /* host Activity */
            mDrawerLayout,                    /* DrawerLayout object */
            R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
            R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
            R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
    )

如果您想获得与 Gmail 完全相同的行为,只需删除 R.drawable.ic_drawer。

 mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        )

您可能需要至少将 appcompat 版本升级到 21。