我像这样使用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这样的兼容性库。
知道为什么会发生这种事吗?
发布于 2014-10-01 18:00:19
应要求:
问题是,您正在尝试检索一个尚未在层次结构中的视图的id。根据活动视图周期,菜单只在onCreate之后创建。最好的解决方案是使用onCreateOptionsMenu,但是即使在这里,R.id.menu_search也不会返回有效的id,因为菜单还没有创建。由于事件的顺序在API 16中发生了变化,我建议您稍加修改它。您可以在onCreate()上创建一个处理程序,并执行一个可运行的postDelayed。该代码将在菜单设计完毕后执行。在这个可运行的视图上,您可以检索R.id.menu_search的视图并突出显示它。
发布于 2016-02-26 14:04:25
我将尝试在这里展示示例代码:
@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;
}注意:工具栏声明为类的成员。
https://stackoverflow.com/questions/26144849
复制相似问题