How to set margins to a custom dialog?

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

    A workaround can be done like this:

    dialog.getWindow().getAttributes().height = 
               (int) (getDeviceMetrics(context).heightPixels*0.8);
    

    `getDeviceMetrics Method:

    public static DisplayMetrics getDeviceMetrics(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getMetrics(metrics);
        return metrics;
    }
    

提交回复
热议问题