如何设置DialogFragment的宽度和高度

如题所述

1. 如果您是直接从资源值转换: int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);

然后在你的布局中指定match_parent的对话框: android:layout_width="match_parent"
android:layout_height="match_parent"

你只需要担心一个地方。它并不完美,但至少它适用于有一个RelativeLayout作为你的对话框的布局文件的根目录。

2. 我有一个固定大小的定义在XML主要布局(LinearLayout中在我的情况)以下内容:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="1000dp"
android:minHeight="450dp"

3. 你可以在下面的代码从Java布局设置宽度和高度。 final AlertDialog alertDialog = alertDialogBuilder.create();
final WindowManager.LayoutParams WMLP = alertDialog.getWindow().getAttributes();

WMLP.gravity = Gravity.TOP;
WMLP.y = mActionBarHeight;
WMLP.x = getResources().getDimensionPixelSize(R.dimen.unknown_image_width);
alertDialog.getWindow().setAttributes(WMLP);

alertDialog.show();

4. 在我的情况下工作的唯一的事情是该解决方案在这里指出:
从阿迪尔博客文章片段: @Override
public void onStart()
{
super.onStart();

// safety check
if (getDialog() == null)
return;

int dialogWidth = ... // specify a value here
int dialogHeight = ... // specify a value here

getDialog().getWindow().setLayout(dialogWidth, dialogHeight);

// ... other stuff you want to do in your onStart() method
}

5. 我固定它设置根布局 int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));
温馨提示:答案为网友推荐,仅供参考