How to set margins to a custom dialog?

前端 未结 15 1201
再見小時候
再見小時候 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

    Step 1. Add the following styles

        
    

    Step 2. Extend custom dialog from DialogFragment and override the following

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setStyle(STYLE_NO_TITLE, R.style.CustomStyle)
        }
    
        override fun onStart() {
            super.onStart()
            val dialog: Dialog? = dialog
            if (dialog != null) {
                val width = ViewGroup.LayoutParams.MATCH_PARENT
                val height = ViewGroup.LayoutParams.MATCH_PARENT
                dialog.window?.setLayout(width, height)
                dialog?.window?.setBackgroundDrawable(InsetDrawable(ColorDrawable(resources.getColor(R.color.transparent, null)), 10, 10, 10, 10))
            }
        }
    

    You will be able to get full screen dialog fragment with margins, without the status bar or overlapping navigation icons.

提交回复
热议问题