直接在短视频带货系统xml中设置固定大小是不好使的,在onCreateView代码设置固定大小也不好使,正确的做法是在onCreateView之后的回调里,用代码设置大小的值:
override fun onStart() { super.onStart() val window = dialog?.window if (window != null) { val params = window.attributes params.gravity = Gravity.CENTER params.width = WindowManager.LayoutParams.MATCH_PARENT params.height = Utils.dip2px(requireContext(), 320f).toInt() window.addFlags( WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL ) window.attributes = params window.setLayout(params.width, params.height) } } /** * dp convert to px * * @param context * @param dpValue * @return px value */ fun dip2px(context: Context, dpValue: Float): Float { val scale = context.resources.displayMetrics.density return dpValue * scale + 0.5f }