How to set margins to a custom dialog?

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

    If you are creating a custom dialog with the background you can prefer the following code. You can change window property MATCH_PARENT for full screen.

    My issue was width of dialog matching to its parent without margin

    try following code as per requirement

        Window window = event_dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);
    

    set these property before dialog.show() OR you can set LEFT,RIGHT,TOP,BOTTOM margin to your dialog by setting margin to your root layout and custom background to its child layout like

    
       
    
       
    
    

提交回复
热议问题