How to set margins to a custom dialog?

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

    I had the same problem and I solved it by setting the window background on an InsetDrawable:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    ...
    ...
    AlertDialog dialog = builder.create();
    
    ColorDrawable back = new ColorDrawable(Color.TRANSPARENT);
    InsetDrawable inset = new InsetDrawable(back, 20);
    dialog.getWindow().setBackgroundDrawable(inset);
    
    dialog.show();
    

    In this case the dialog will appear with a margin of 20 to all edges.

提交回复
热议问题