這是一個直接的,程序化的方式來設置我的對話框的邊緣位置和大小。
我測試了我的一個DialogFragment
方法,通過在onCreateDialog
方法應用它:
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// create dialog in an arbitrary way
Dialog dialog = super.onCreateDialog(savedInstanceState);
DialogUtils.setMargins(dialog, 0, 150, 50, 75);
return dialog;
}
這是應用邊距對話框的方法:
public static Dialog setMargins(Dialog dialog, int marginLeft, int marginTop, int marginRight, int marginBottom)
{
Window window = dialog.getWindow();
if (window == null)
{
// dialog window is not available, cannot apply margins
return dialog;
}
Context context = dialog.getContext();
// set dialog to fullscreen
RelativeLayout root = new RelativeLayout(context);
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
// set background to get rid of additional margins
window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
// apply left and top margin directly
window.setGravity(Gravity.LEFT | Gravity.TOP);
LayoutParams attributes = window.getAttributes();
attributes.x = marginLeft;
attributes.y = marginTop;
window.setAttributes(attributes);
// set right and bottom margin implicitly by calculating width and height of dialog
Point displaySize = getDisplayDimensions(context);
int width = displaySize.x - marginLeft - marginRight;
int height = displaySize.y - marginTop - marginBottom;
window.setLayout(width, height);
return dialog;
}
這裏是輔助方法我使用:
@NonNull
public static Point getDisplayDimensions(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
// find out if status bar has already been subtracted from screenHeight
display.getRealMetrics(metrics);
int physicalHeight = metrics.heightPixels;
int statusBarHeight = getStatusBarHeight(context);
int navigationBarHeight = getNavigationBarHeight(context);
int heightDelta = physicalHeight - screenHeight;
if (heightDelta == 0 || heightDelta == navigationBarHeight)
{
screenHeight -= statusBarHeight;
}
return new Point(screenWidth, screenHeight);
}
public static int getStatusBarHeight(Context context)
{
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return (resourceId > 0) ? resources.getDimensionPixelSize(resourceId) : 0;
}
public static int getNavigationBarHeight(Context context)
{
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return (resourceId > 0) ? resources.getDimensionPixelSize(resourceId) : 0;
}
輔助方法是在我的另一個SO answers中解釋。
這個Gist包含支持沉浸模式的擴展版本。
如果在對話框樣式中使用dialog_background_inset drawable作爲windowBackground,並且不使用背景作爲對話框佈局,則不必擔心在佈局中包含額外的填充以允許插入。 – ChrisJD 2012-09-05 06:08:01
@ChrisJD我無法得到它的工作。無論如何,pospi解決方案正在工作。有時真的很奇怪Android需要多少奇怪的代碼來實現非常簡單的事情。 – Piotr 2012-09-27 10:45:30
@pospi你是否知道如何在對話框內容之外點擊時取消它?因爲它無法使用您的解決方案。 – Piotr 2012-09-27 11:29:47