2

我像这样使用 ShowcaseView:

ActionItemTarget target = new ActionItemTarget(this, R.id.menu_search);

ShowcaseView sv = new ShowcaseView.Builder(this, true)
                .setTarget(target)
                .setContentText("Press this to search")
                .setStyle(R.style.CustomShowcaseTheme)
                .build();

但是当我第一次启动应用程序时,ShowcaseView 会突出显示主页按钮。当我尝试再次启动应用程序时,会显示正确的 ActionBar 项目。我的应用不使用 ActionbarSherlock 等兼容性库。

知道为什么会发生这种情况吗?

4

2 回答 2

3

按照要求:

问题是您正在尝试检索尚未在层次结构中的视图的 id。根据活动视图周期,菜单仅在之后创建onCreate。最好的解决方案是使用onCreateOptionsMenu,但即使在这里R.id.menu_search也不会返回有效的 id,因为尚未创建菜单。由于 API lvl 16 中事件的顺序发生了变化,我建议你稍微修改一下。您可以在您的上创建一个处理程序onCreate()并执行一个 postDelayed 可运行文件。代码将在菜单设计完成后执行。在该可运行文件上,您可以检索视图R.id.menu_search并突出显示它。

于 2014-10-01T18:00:19.933 回答
0

我将尝试在这里展示示例代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    new ShowcaseView.Builder(this)
            .withMaterialShowcase()
            .setTarget(new ToolbarActionItemTarget(this.toolbar, R.id.menu_search))
            .setContentText("Here's how to highlight items on a toolbar")
            .build()
            .show();

    return true;
}

注意:工具栏声明为类的成员。

于 2016-02-26T14:04:25.123 回答