如何隐藏状态&弹出菜单期间的导航栏? [Kotlin]
到目前为止,这是我的进步。问题在于,菜单启动时,状态栏会短暂闪烁,并且在菜单和选择期间,导航栏保留。
是否有人知道如何在不显示系统栏的完整应用程序中显示弹出菜单?
class MainActivity : AppCompatActivity() {
lateinit var popup2:View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
popup2 = findViewById<Button>(R.id.popup_press)
supportActionBar?.hide() //Doesn't work
popup2.setOnClickListener {
val popup = PopupMenu(applicationContext, popup2)
popup.menuInflater.inflate(R.menu.popupmenu, popup.menu)
popup.setOnMenuItemClickListener { i ->
when (i.itemId) {
R.id.menuA -> {
Toast.makeText(applicationContext, "Menu A", Toast.LENGTH_LONG).show()
}
R.id.menuB -> {
Toast.makeText(applicationContext, "Menu B", Toast.LENGTH_LONG).show()
}
R.id.menuC -> {
Toast.makeText(applicationContext, "Menu C", Toast.LENGTH_LONG).show()
}
else -> false
}
true
}
popup.show()
}
}
//Hides status bar AFTER menu has shown. Can't stop it showing!!!!!
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUIView(popup2) //runs before menu and after
} else {
hideSystemUIView(getWindow().getDecorView()) // Hides menu - blinks
}
}
fun hideSystemUIView(view: View) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
window.insetsController?.apply {
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/popup_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="POP UP MENU"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuA"
android:title="menu A">
</item>
<item
android:id="@+id/menuB"
android:title="menu B">
</item>
<item
android:id="@+id/menuC"
android:title="menu C">
</item>
</menu>
This is my progress so far. The problem with this is that the status bar flashes briefly when the menu starts, and the navigation bar stays present during the menu and selection.
Is anyone aware of how to make a popup menu display in a fullscreen application without the system bars ever showing?
class MainActivity : AppCompatActivity() {
lateinit var popup2:View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
popup2 = findViewById<Button>(R.id.popup_press)
supportActionBar?.hide() //Doesn't work
popup2.setOnClickListener {
val popup = PopupMenu(applicationContext, popup2)
popup.menuInflater.inflate(R.menu.popupmenu, popup.menu)
popup.setOnMenuItemClickListener { i ->
when (i.itemId) {
R.id.menuA -> {
Toast.makeText(applicationContext, "Menu A", Toast.LENGTH_LONG).show()
}
R.id.menuB -> {
Toast.makeText(applicationContext, "Menu B", Toast.LENGTH_LONG).show()
}
R.id.menuC -> {
Toast.makeText(applicationContext, "Menu C", Toast.LENGTH_LONG).show()
}
else -> false
}
true
}
popup.show()
}
}
//Hides status bar AFTER menu has shown. Can't stop it showing!!!!!
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUIView(popup2) //runs before menu and after
} else {
hideSystemUIView(getWindow().getDecorView()) // Hides menu - blinks
}
}
fun hideSystemUIView(view: View) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
window.insetsController?.apply {
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
}
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/popup_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="POP UP MENU"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuA"
android:title="menu A">
</item>
<item
android:id="@+id/menuB"
android:title="menu B">
</item>
<item
android:id="@+id/menuC"
android:title="menu C">
</item>
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何从大象中摔下来?
你没有;你从鸭子那里下来。
使用这种相同的理念,我认为不可能纠正/改变内置菜单功能的行为。我的解决方案是废除这些问题,只是构建自己的菜单功能,这实际上只需要线性布局中的一些标准化文本字段即可。现在,我可以完全控制发生的事情。
这是一个示例字段:
How do you get down from an elephant?
You don't; you get down from a duck.
Using this same philosophy I have considered that it isn't possible to correct/alter the behaviour of the in-built menu functions. My solution has been to scrap those and just build my own menu capability which really just needed a few standardised text fields in a Linear layout. Now I have full control over what happens.
Here is an example field: