Android Chrome 当片段有选项菜单时,演员介绍叠加层有不正确的行为
Android Chrome Cast Introduction Overlay has incorrect behaviour when fragment has options menu
我将 Cast 按钮显示为从 activity 扩展而来的选项菜单项,但我注意到当 activity 有一个子片段并且子片段没有选项菜单项本身,chrome 演员表介绍叠加层工作正常。但是,当片段有自己的选项菜单时,Cast 介绍叠加层无法正常工作,它要么显示在左上角,要么显示在正确的位置但没有突出显示 Cast 图标。
这是初始化覆盖的代码
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
loadCastButton(menu)
return super.onCreateOptionsMenu(menu)
}
private fun loadCastButton(menu: Menu?) {
menuInflater.inflate(R.menu.menu_cast, menu)
CastButtonFactory.setUpMediaRouteButton(applicationContext, menu, R.id.cast_menu_item)
val mediaRoutebutton = menu?.findItem(R.id.cast_menu_item)?.actionView as? MediaRouteButton
mediaRoutebutton?.dialogFactory = CastDialogFactory()
handleCastTutorial(menu)
}
private fun handleCastTutorial(menu: Menu?) {
val castButton = menu?.findItem(R.id.cast_menu_item)
if (castButton == null) {
return
}
castViewModel.isCastingAvailable.observe(this) {
if (it == true && castButton.isVisible) {
//Show cast tutorial
castViewModel.setCastTutorialShown(true)
IntroductoryOverlay.Builder(this, castButton)
.setTitleText(R.string.cast_tutorial_title)
.setSingleTime()
.build()
.show()
}
}
}
当您在片段和活动中显示 Cast 按钮时,菜单随处膨胀,Cast 按钮在其中一个片段或活动中初始化,然后立即再次隐藏。我推荐的解决方案是稍微延迟演员教程,然后再次检查可见性和 window 附加状态:
if (!castViewModel.getCastTutorialShown()) {
binding.root.postDelayed(200L) {
// Check if it is still visible.
if (castButton.isVisible && castButton.actionView.isAttachedToWindow && !castViewModel.getCastTutorialShown()) {
castViewModel.setCastTutorialShown(true)
IntroductoryOverlay.Builder(this, castButton)
.setTitleText(R.string.cast_tutorial_title)
.setSingleTime()
.build()
.show()
}
}
}
我将 Cast 按钮显示为从 activity 扩展而来的选项菜单项,但我注意到当 activity 有一个子片段并且子片段没有选项菜单项本身,chrome 演员表介绍叠加层工作正常。但是,当片段有自己的选项菜单时,Cast 介绍叠加层无法正常工作,它要么显示在左上角,要么显示在正确的位置但没有突出显示 Cast 图标。
这是初始化覆盖的代码
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
loadCastButton(menu)
return super.onCreateOptionsMenu(menu)
}
private fun loadCastButton(menu: Menu?) {
menuInflater.inflate(R.menu.menu_cast, menu)
CastButtonFactory.setUpMediaRouteButton(applicationContext, menu, R.id.cast_menu_item)
val mediaRoutebutton = menu?.findItem(R.id.cast_menu_item)?.actionView as? MediaRouteButton
mediaRoutebutton?.dialogFactory = CastDialogFactory()
handleCastTutorial(menu)
}
private fun handleCastTutorial(menu: Menu?) {
val castButton = menu?.findItem(R.id.cast_menu_item)
if (castButton == null) {
return
}
castViewModel.isCastingAvailable.observe(this) {
if (it == true && castButton.isVisible) {
//Show cast tutorial
castViewModel.setCastTutorialShown(true)
IntroductoryOverlay.Builder(this, castButton)
.setTitleText(R.string.cast_tutorial_title)
.setSingleTime()
.build()
.show()
}
}
}
当您在片段和活动中显示 Cast 按钮时,菜单随处膨胀,Cast 按钮在其中一个片段或活动中初始化,然后立即再次隐藏。我推荐的解决方案是稍微延迟演员教程,然后再次检查可见性和 window 附加状态:
if (!castViewModel.getCastTutorialShown()) {
binding.root.postDelayed(200L) {
// Check if it is still visible.
if (castButton.isVisible && castButton.actionView.isAttachedToWindow && !castViewModel.getCastTutorialShown()) {
castViewModel.setCastTutorialShown(true)
IntroductoryOverlay.Builder(this, castButton)
.setTitleText(R.string.cast_tutorial_title)
.setSingleTime()
.build()
.show()
}
}
}