对话框在某些设备上未填满整个窗口

时间:2019-02-12 01:22:24

标签: android android-layout kotlin

当我摆弄Google I/O 2018 Android App时,我注意到在我的设备(小米Mi5,Android 7.0)上,他们的对话框使整个屏幕变暗,除了状态栏( white ),如屏幕截图所示:

screen1

它不会在模拟器上发生(已在6.0、7.0、8.0上测试)。

我查看了视图层次结构,我注意到DecorView的子级的顶部填充设置为60,而在模拟器上,其顶部填充设置为0。

screen2-此FrameLayout的边界

这是对话框的实现: 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))
    }
}

有人知道如何解决吗?

1 个答案:

答案 0 :(得分:0)

尝试一下,它将为您服务

    WindowManager manager = (WindowManager) 
    getActivity().getSystemService(Activity.WINDOW_SERVICE);
    int width;
    width = manager.getDefaultDisplay().getWidth();
    pauseDialog.getWindow().setBackgroundDrawable(null);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(pauseDialog.getWindow().getAttributes());
    lp.width = width;
    lp.gravity = Gravity.CENTER;
    pauseDialog.getWindow().setAttributes(lp);