我正在尝试实现一个全屏应用程序,其中用户无权访问状态栏和导航栏。
最好我希望它们被完全移除,但据我所知,这是不可能的,除非你对设备进行 root
所以我的问题基本上是:如何在显示弹出菜单时隐藏导航栏图标?
之前
之后
到目前为止,我已经尝试过:
- 在显示弹出菜单之前和之后调用
hideNavigation()
- 在
onCreate()、onResume() 和 onWindowFocusChanged() 中调用
hideNavigation()
- 请求关注另一个 View
- 尝试从下拉列表中清除焦点(尝试失败,并没有真正找到方法)
- 更改图标颜色,“假装”它会被隐藏(尝试失败,并没有真正找到这样做的方法)
- 将
hideNavigation()
与Handler
结合使用(尝试失败,可能是我没做对) - 尝试配置一些 COSU/KIOSK 模式选项(也许有某种方法可以完全禁用整个导航栏?我还没有找到隐藏后退按钮的方法)
Activity
class PinCodeActivity, HasTitleBar {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pin_code)
initTitleBarWith(this)
hideNavigation()
}
override fun onResume() {
super.onResume()
hideNavigation()
}
fun hideNavigation() {
window.decorView.apply {
systemUiVisibility = FLAGS
}
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
hideNavigation()
}
}
const val FLAGS = (View.SYSTEM_UI_FLAG_LOW_PROFILE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
标题栏
fun HasTitleBar.initTitleBarWith(activity: Activity, resId: Int = R.id.titleBar) {
val langButton = activity.findViewById<View>(resId).findViewById<Button>(R.id.tbLanguageChoiceBtn)
val wrapper = ContextThemeWrapper(activity, R.style.MyPopupMenu)
val popupMenu = PopUpLanguageMenu(wrapper, langButton)
langButton.setOnClickListener {
activity.hideNavigation()
popupMenu.showMenu()
activity.hideNavigation()
}
}
弹出式菜单
class PopUpLanguageMenu constructor(context: Context, view: View) : PopupMenu(context, view) {
private var popupHelper: MenuPopupHelper
init {
val popUpMenu = PopupMenu(context, view).apply {
inflate(R.menu.menu_language_dropdown)
}
popupHelper = MenuPopupHelper(context, popUpMenu.menu as MenuBuilder, view)
popupHelper.run {
gravity = Gravity.END
setForceShowIcon(true)
}
}
fun showMenu() {
popupHelper.show()
}
}
预期结果:导航栏及其图标被隐藏,弹出菜单显示后,图标仍然HIDDEN
实际结果:导航栏及其图标被隐藏,弹出菜单显示后,图标SHOWN
最佳答案
导航栏重新出现是因为在 Views 堆栈的顶部新绘制了一个新的 DecorView (PopupDecorView),它不受您之前设置的 FLAGs 的影响。
这里没有 Elixir ,我的方法是通过反射深入 WindowManagerGlobal 并捕获 peek View,再次在其上应用系统 FLAG,因此在 PopupMenu 出现后,它设法隐藏导航栏右侧在那之后(仍然有一次从导航栏中显示)。
代码如下:
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideNavigation()
} else {
// When PopupMenu appears, the current Activity looses the focus
setFlagsOnThePeekView() // Hijack to the current peek view, apply the Flags on it
}
}
@SuppressLint("PrivateApi")
fun setFlagsOnThePeekView() {
try {
val wmgClass = Class.forName("android.view.WindowManagerGlobal")
val wmgInstance = wmgClass.getMethod("getInstance").invoke(null)
val viewsField = wmgClass.getDeclaredField("mViews")
viewsField.isAccessible = true
val views = viewsField.get(wmgInstance) as ArrayList<View>
// When the popup appears, its decorView is the peek of the stack aka last item
views.last().apply {
systemUiVisibility = FLAGS
setOnSystemUiVisibilityChangeListener {
systemUiVisibility = FLAGS
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
关于android - 打开弹出菜单时如何修复 : Navigation bar icons still showing,(全屏 - 沉浸式粘性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140793/