Tob*_*uts 5 android android-fullscreen android-immersive android-navigation-bar
我正在尝试实现一个全屏应用程序,其中用户无法访问状态栏和导航栏。
最好我希望它们被完全删除,但从我读到的情况来看,这是不可能的,除非你root设备
所以我的问题基本上是:当我显示弹出菜单时,如何隐藏导航栏图标?
到目前为止,我已经尝试过:
hideNavigation()显示弹出菜单之前和之后调用hideNavigation()onCreate(), onResume() & onWindowFocusChanged()hideNavigation()结合使用(Handler失败的尝试,可能是我没做对)
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)
Run Code Online (Sandbox Code Playgroud)
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()
}
}
Run Code Online (Sandbox Code Playgroud)
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()
}
}
Run Code Online (Sandbox Code Playgroud)
预期结果:导航栏及其图标被隐藏,显示弹出菜单后,图标仍然隐藏
实际结果:导航栏及其图标被隐藏,显示弹出菜单后,显示图标
导航栏重新出现是因为在视图堆栈的顶部新绘制了一个新的 DecorView (PopupDecorView),它不受之前设置的 FLAG 的影响。
这里没有灵丹妙药,我的方法是通过反射深入 WindowManagerGlobal 并捕获 peek View,再次在其上应用系统标志,因此在 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()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2570 次 |
| 最近记录: |