工具栏导航按钮

时间:2015-01-28 09:04:17

标签: android android-actionbar navigation-drawer toolbar android-button

这是我的工具栏: my toolbar

这是Gmail应用工具栏: gmail toolbar

我想要打开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 */
        )

提前致谢!

4 个答案:

答案 0 :(得分:1)

在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添加它并给它动画,当抽屉打开时它变换为后退按钮

请参阅此链接Drawer Arrow Drawable

答案 1 :(得分:0)

您可以使用以下样式:

<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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您正在使用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。