【发布时间】:2019-02-12 14:44:28
【问题描述】:
当我在摆弄Google I/O 2018 Android App 时,我注意到在我的设备(小米 Mi5,Android 7.0)上,除了(white)状态栏外,它们的对话框会使整个屏幕变暗,如图所示截图:
它不会出现在模拟器上(在 6.0、7.0、8.0 上测试)。
我查看了视图层次结构,发现 DecorView 的子级将其顶部填充设置为 60,而在模拟器上则设置为 0。
- 这个 FrameLayout 的边界
这是他们对 Dialog 的实现: https://github.com/google/iosched/blob/master/mobile/src/main/java/com/google/samples/apps/iosched/widget/CustomDimDialog.kt
class CustomDimDialog(context: Context?) : AppCompatDialog(context, R.style.Theme_IOSched_Dialog) {
init {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window?.run {
// Spread the dialog as large as the screen.
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
override fun setContentView(view: View?) {
if (view != null) {
super.setContentView(wrap(view))
}
}
private fun wrap(content: View): View {
val res = context.resources
val verticalMargin = res.getDimensionPixelSize(R.dimen.dialog_vertical_margin)
val horizontalMargin = res.getDimensionPixelSize(R.dimen.dialog_horizontal_margin)
return FrameLayout(context).apply {
addView(content, FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
).apply {
setMargins(horizontalMargin, verticalMargin, horizontalMargin, verticalMargin)
gravity = Gravity.CENTER
})
val rect = Rect()
setOnTouchListener { _, event ->
when (event.action) {
// The FrameLayout is technically inside the dialog, but we treat it as outside.
MotionEvent.ACTION_DOWN -> {
content.getGlobalVisibleRect(rect)
if (!rect.contains(event.x.toInt(), event.y.toInt())) {
cancel()
true
} else {
false
}
}
else -> {
false
}
}
}
background = ColorDrawable(ResourcesCompat.getColor(res, R.color.scrim, context.theme))
}
}
有人知道怎么解决吗?
【问题讨论】:
标签: android android-layout kotlin