如何用自定义drawable替换Android工具栏上用于ActionBarToggle的汉堡包图标?

时间:2014-11-19 22:52:35

标签: android navigation-drawer android-toolbar

我使用Android 5.0中的新工具栏实现了一个基本的ActionBarDrawerToggle。

但是,我无法弄清楚如何更改提供的默认汉堡包图标。 从android文档中可以看出"给定的Activity将链接到指定的DrawerLayout,工具栏的导航图标将被设置为自定义drawable ...这个drawable在抽屉关闭时显示汉堡图标抽屉打开时的箭头。当抽屉打开时,它会在这两种状态之间激活。"

我目前使用以下代码正常工作,但是我想用我自己的drawable替换默认提供的汉堡包。

这是我目前的代码:

MainActivity.java

@InjectView(R.id.main_activity_toolbar)
Toolbar mToolbar;

@InjectView(R.id.main_activity_drawer_layout)
DrawerLayout mDrawerLayout;

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

    setSupportActionBar(mToolbar);
    mToolbar.setNavigationIcon(R.drawable.navigation);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

这一行:

mToolbar.setNavigationIcon(R.drawable.navigation);

似乎不起作用。

这可能吗?谢谢!

ActionBarToggle文档 - https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

10 个答案:

答案 0 :(得分:25)

这两行代码对我有用:

DataRow newRow = ds.Tables[tableToAdd].Rows.Add();
newRow["partno"] = ds.Tables[tableToAdd].Rows[i - 1]["partno"];

然后打电话给:

String string="I would loved to be the part of cricket team"

答案 1 :(得分:24)

您可以将工具栏用作独立模式,这意味着您使用工具栏作为 ActionBarDrawerToggle 构造函数的一部分,您可以使用以下代码实现这一点:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, null,
                       R.drawable.appbar, R.drawable.appbar)

(注意工具栏实例未如何发送到ActionBarDrawerToggle构造函数)

此外,您应手动充气菜单

mToolbar = (Toolbar) findViewById(R.id.nav_toolbar);
mToolbar.inflateMenu(R.menu.base);

删除 setSupportActionBar(mToolbar); 代码行。

当然,您必须自己处理导航点击:

mToolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() ...

然后,您可以这样打开您的抽屉:

drawerButton = (BadgeDrawerButton) findViewById(R.id.badge_drawer_button);
drawerButton.setOnClickListener(
                       new View.OnClickListener() {

                              @Override
                              public void onClick(View v) {
                                     mDrawerLayout.openDrawer(Gravity.LEFT);
                              }
                       });

希望这可能会有所帮助。

答案 2 :(得分:7)

我的解决方案是继承ActionBarDrawerToggle。

public class MyActionBarDrawerToggle extends android.support.v7.app.ActionBarDrawerToggle {

    public MyActionBarDrawerToggle(Activity activity, final DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);

        setHomeAsUpIndicator(R.drawable.drawer_toggle);
        setDrawerIndicatorEnabled(false);

        setToolbarNavigationClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.LEFT);
            }
        });
     }
}

答案 3 :(得分:7)

ActionBarDrawerToggle的默认菜单是DrawerArrowDrawable。

您可以将其子类化以添加自定义功能,例如徽章,如下所示:

<my-textarea @keyup="update()" @change="somethingElse()"></my-textarea>

用法:

<li>

答案 4 :(得分:6)

对我有用的是我只需要拨打toolbar.setNavigationIcon(R.drawable.ic_camera_alt_24dp); 在onCreate结束时,或者至少在mDrawerToggle = new ActionBarDrawerToggle...之后

答案 5 :(得分:2)

对于v7支持库 - 您可以创建自己的DrawerArrowDrawable表示。

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

            public void onDrawerClosed(View view) {
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                supportInvalidateOptionsMenu();
            }
        };
        mDrawerToggle.setDrawerIndicatorEnabled(true);

        DrawerArrowDrawable drawerArrowDrawable = new DrawerArrowDrawable(this);
        drawerArrowDrawable.setAlpha(1);
        drawerArrowDrawable.setSpinEnabled(false);
        drawerArrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_LEFT);
        drawerArrowDrawable.setColor(Color.BLACK);

        mDrawerToggle.setDrawerArrowDrawable(drawerArrowDrawable);

答案 6 :(得分:2)

截至2018年1月,这是一个有效的解决方案(至少对我而言):

    //setSupportActionBar(toolbar)

    val toggle = ActionBarDrawerToggle(this, drawer_layout, null, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    toolbar.inflateMenu(R.menu.menu_main)
    toolbar.setNavigationIcon(R.drawable.ic_menu)
    toolbar.setNavigationOnClickListener {
        drawer_layout.openDrawer(Gravity.START)
    }

    toolbar.setOnMenuItemClickListener {
        true
    }

    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()

    nav_view.setNavigationItemSelectedListener(this)

需要关注的事项:

  • setSupportActionBar应该被注释掉
  • ActionBarDrawerToggle未提及toolbar
  • 我们应该自己夸大菜单,并处理onClicks。 onCreateOptionsMenuonOptionsItemSelected将无效。
  • 别忘了拨打toggle.syncState()

答案 7 :(得分:1)

以下是我最终能够让我的工作方式。

private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar);

if (toolbar != null) {
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout,
                toolbar,
                R.string.drawer_open,
                R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

原来是

mDrawerToggle.syncState();

最终让一切顺利。

答案 8 :(得分:0)

我认为建议在syncState()生命周期方法中调用onPostCreate(...)

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

答案 9 :(得分:0)

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);

toggle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_action_name);