Android ShowcaseView突出显示错误的项目
我使用ShowcaseView的方式如下:Android ShowcaseView突出显示错误的项目,android,showcaseview,Android,Showcaseview,我使用ShowcaseView的方式如下: ActionItemTarget target = new ActionItemTarget(this, R.id.menu_search); ShowcaseView sv = new ShowcaseView.Builder(this, true) .setTarget(target) .setContentText("Press this to search")
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会突出显示home按钮。当我再次尝试启动应用程序时,会显示正确的ActionBar项。
我的应用程序不使用ActionbarSherlock之类的兼容性库
知道为什么会发生这种情况吗?根据要求:
问题是您正在尝试检索尚未在层次结构中的视图的id。根据活动视图循环,菜单仅在创建后创建。最好的解决方案是使用
onCreateOptions菜单
,但即使在这里R.id.menu\u search
也不会返回有效的id,因为菜单尚未创建。由于APIL16中的事件顺序发生了变化,我建议您稍微修改一下。您可以在onCreate()
上创建一个处理程序,并执行postDelayed runable。该代码将在菜单设计完成后执行。在该runable上,您可以检索R.id.menu\u search
的视图并将其高亮显示。我将尝试在此处演示示例代码:
@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;
}
注意:工具栏声明为该类成员。您所说的“再次启动应用程序”是什么意思。您是强制关闭它并打开它,还是调用onPause+onResume重新打开它?你什么时候调用这个代码?因为
R.id.menu\u search
此时可能还不可用time@PedroOliveira我在onCreate()中调用它。显然,这是一个错误的地方。使用此代码的最佳位置是OnCreateOptions菜单,即使在那里,R.id.menu\u search
也没有指向视图,因为它还没有膨胀,所以你必须找到一种方法,在那里不使用id。也许你可以发布一个答案,这样我就可以接受它。