首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShowcaseView高亮显示错误项

ShowcaseView高亮显示错误项
EN

Stack Overflow用户
提问于 2014-10-01 15:24:58
回答 2查看 556关注 0票数 2

我像这样使用ShowcaseView:

代码语言:javascript
复制
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这样的兼容性库。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-01 18:00:19

应要求:

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

票数 3
EN

Stack Overflow用户

发布于 2016-02-26 14:04:25

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

代码语言:javascript
复制
@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;
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26144849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档