获取参考抽屉切换,支持动作条 [英] Get reference to drawer toggle in support actionbar

查看:817
本文介绍了获取参考抽屉切换,支持动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 ShowcaseView 库的应用程序的教程。我需要一个参考抽屉式导航栏切换按钮(又名汉堡按钮):

I use ShowcaseView library for app tutorial. I need to get a reference to Navigation Drawer toggle button(aka "burger button"):

我使用工具栏的动作条,我不知道如何得到这个按钮。通常切换抽屉里我用这样的:

I use Toolbar as Actionbar, and I have no idea how to get this button. Usually to toggle drawer I use this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            toggleDrawer(Gravity.START);
        }
}

但是当我使用设备监视器,使屏幕快照,有ID为家没有意见。

But when I use Device Monitor to make snapshot of the screen, there's no view with id "home".

有什么建议?

推荐答案

在原始的动作条,两个查看在你的左边通常显示的是向上查看首页查看。在向上查看是,通常会显示汉堡包图标的按钮,而首页查看显示应用程序图标。事情已经用更改工具栏和材料设计;不再有一个默认的查看 android.R.id.home ID。因此,您不再需要检查一个菜单项所述的ID onOptionsItemSelected()方法。

In the original ActionBar, the two Views commonly shown on the left are the Up View and the Home View. The Up View is the button that usually displays the "hamburger" icon, while the Home View displays the app icon. Things have changed with Toolbars and Material Design; there is no longer a default View with the android.R.id.home ID. As such, you no longer need to check for a MenuItem with said ID in the onOptionsItemSelected() method.

目前工具栏类现在指的向上查看内部的导航按钮查看,并且,如果需要的话,你可以得到一个参考,可以与以下的code。

The current Toolbar class now refers to the Up View internally as the Nav Button View, and, if needed, you can get a reference to it with the following code.

工具栏类创建它的子查看取值动态,所以你必须要搜索的导航按钮查看 - 即汉堡按钮 - 你自己。在你设置的切换,但你添加任何其他的ImageButton S到工具栏的<前EM>导航按钮的查看工具栏的唯一的ImageButton 孩子,你可以得到像这样:

The Toolbar class creates its child Views dynamically, so you'll have to search for the Nav Button View - i.e., the "burger button" - yourself. After you've set the toggle, but before you've added any other ImageButtons to the Toolbar, the Nav Button View is the only ImageButton child of the Toolbar, which you can get like so:

private ImageButton getNavButtonView(Toolbar toolbar)
{
    for (int i = 0; i < toolbar.getChildCount(); i++)
        if(toolbar.getChildAt(i) instanceof ImageButton)
            return (ImageButton) toolbar.getChildAt(i);

    return null;
}

如果您需要参考应用程序图标的查看 - 这相当于旧的首页查看 - 你可以用类似的方法寻找一个的ImageView

If you need a reference to the app icon's View - that which corresponds to the old Home View - you can use a similar method looking for an ImageView.

这篇关于获取参考抽屉切换,支持动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆