获取参考抽屉切换,支持动作条 [英] Get reference to drawer toggle in support actionbar
问题描述
我用 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 View
s 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 Toolbar
s 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 View
s 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 ImageButton
s 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屋!