简体   繁体   中英

How can I still display a full screen after a popup menu displyed in Android?

When I run the Code A , the app is displayed as a full screen, you can see the Image 1 .

After I click the button1, a popup menu is displayed, but the full screen disappear! you can see the Image 2 .

How can I still display a full screen after a popup menu displyed in Android?

Code A

const val FLAGS_FULLSCREEN = View.SYSTEM_UI_FLAG_LOW_PROFILE or
        View.SYSTEM_UI_FLAG_FULLSCREEN or
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

private const val IMMERSIVE_FLAG_TIMEOUT = 500L

class UIHome : AppCompatActivity() {

    private lateinit var mContext: Context
    private lateinit var container: ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_home)
        container = findViewById(R.id.camera_ui_container)

        button1.setOnClickListener{
           v -> showPopup(v, this)
        }

    }

    override fun onResume() {
        super.onResume()
        container.postDelayed({
            container.systemUiVisibility = FLAGS_FULLSCREEN
        }, IMMERSIVE_FLAG_TIMEOUT)
    }


    private fun showPopup(v: View, mContext: Context) {
        val popup = PopupMenu(mContext, v)
        popup.inflate(R.menu.menu_popup_more)

        popup.setOnMenuItemClickListener {
            item -> handlePopupMenu(item, mContext)
        }
        popup.show()
    }

    private fun handlePopupMenu(item: MenuItem, mContext: Context): Boolean {
        when (item.itemId) {
            R.id.MenuMoreAbout->{
               openActivity<UIAbout>()
            }
        }
        return false
    }

}

Layout

<?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:id="@+id/camera_ui_container"
        android:layout_width="match_parent"
        android:background="@android:color/black"
        android:layout_height="match_parent">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:text="Popup Menu"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"></Button>

</androidx.constraintlayout.widget.ConstraintLayout>

Image 1

在此处输入图像描述

Image 2

在此处输入图像描述

Added Content

To Ankit: Thanks, Your code works, but the UI still display action bar, you can see Image 3 .

Image 3

在此处输入图像描述

Add this in oncreate for fullscreen activity

override fun onCreate(savedInstanceState: Bundle?) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

Call hideSystemUI from onWindowFocusChanged because from onresume it will not take effect.

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    hideSystemUI()
}    


 private fun hideSystemUI() {
    // Enables regular immersive mode.
    // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
    // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

you can check this docs also https://developer.android.com/training/system-ui/immersive.html#kotlin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM