添加全屏对话框示例

全屏对话框
This commit is contained in:
yutou 2022-05-23 19:37:59 +08:00
parent 62fb58b0fe
commit 841ab45269

48
Android/Demo1/MyDialog.kt Normal file
View File

@ -0,0 +1,48 @@
import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Build
import android.view.Display
import android.view.Window
import android.view.WindowInsets
import android.view.WindowManager
class MyDialog(context: Activity) : Dialog(context) {
private val activityContext:Activity= context
private fun initView() {
//获取display
val display: Display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.display!!
} else {
activityContext.windowManager.defaultDisplay
}
//Android R后设置全屏的方式变了
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.R) {
//全屏
window!!.insetsController?.hide(WindowInsets.Type.statusBars())
//保留状态栏
window!!.insetsController?.hide(WindowInsets.Type.navigationBars())
}else{
window!!.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
//设置LayoutParams
window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT)
//设置attribute
val attributes = window?.attributes!!
attributes.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
window!!.attributes = attributes
//设置高宽及padding
window!!.decorView.minimumHeight=display.mode.physicalHeight
window!!.decorView.minimumWidth=display.mode.physicalWidth+1440
//如果未设置背景颜色或设置在padding之后,则无法全屏,这里设置的透明底
window!!.decorView.setBackgroundColor(Color.parseColor("#10000000"))
window!!.decorView.setPadding(0,0,0,0)
}
override fun show() {
//在展示前设置参数
initView()
super.show()
}
}