How to set margins to a custom dialog?

前端 未结 15 1218
再見小時候
再見小時候 2020-12-05 05:56

Does anybody knows how can I set margins to a custom dialog? I\'m asking because I\'ve a custom dialog but when displayed it stretches to fill the parent, even though I set

15条回答
  •  佛祖请我去吃肉
    2020-12-05 06:35

    if you extend Dialog Fragment, put this:

    override fun onStart() {
            super.onStart()
            if (dialog != null) {
                dialog!!.getWindow()!!.setLayout(Constraints.LayoutParams.MATCH_PARENT, Constraints.LayoutParams.WRAP_CONTENT) // full width dialog
                val back = ColorDrawable(Color.TRANSPARENT)
                val margin = 16
                val inset = InsetDrawable(back, margin)
                dialog!!.window!!.setBackgroundDrawable(inset)
            }
        }
    

    layoutparams is depend on your dialog parent view

提交回复
热议问题