我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
更简单的是这样做:
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
alertDialog.getWindow().setLayout(width, height);
其他回答
public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
{
try
{
Display display = activity.getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = (int) (width - (width * 0.07) );
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
return layoutParams;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
部分基于阿南德的回答。这对我来说很管用:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val fragmentActivity = requireActivity()
val v = View.inflate(context, R.layout.fragment_about_dialog, null)
val dialog = Dialog(fragmentActivity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(v)
val wm = fragmentActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = if (VERSION.SDK_INT >= VERSION_CODES.R) {
fragmentActivity.display
} else {
wm.defaultDisplay // deprecated in API 30
}
val size = Point()
display?.getSize(size)
val width = size.x - 50
val height = size.y - 50
val lp = WindowManager.LayoutParams()
lp.copyFrom(dialog.window?.attributes)
lp.width = width
lp.height = height
dialog.show()
dialog.window?.attributes = lp
return dialog
}
对于对话框布局使用constraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/dialogLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</androidx.constraintlayout.widget.ConstraintLayout>
结果:
这在改变屏幕方向时工作得很好。
***In Kotlin You can Code like This : -***
fun customDialog(activity: Activity?, layout: Int): Dialog {
val dialog = Dialog(activity!!)
try {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(layout)
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.window!!.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
dialog.show()
} catch (e: Exception) {
}
return dialog
}
下面是我的自定义对话框宽度的变体:
DisplayMetrics displaymetrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels * (ThemeHelper.isPortrait(mContext) ? 0.95 : 0.65));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width;
getWindow().setAttributes(params);
因此,根据设备方向(ThemeHelper.isPortrait(mContext))对话框的宽度将是95%(纵向模式)或65%(横向模式)。这比作者要求的要多一点,但对某些人来说可能有用。
你需要创建一个从Dialog扩展而来的类,并把这段代码放到你的onCreate(Bundle savedInstanceState)方法中。
对于对话框的高度,代码应该类似于此。
我的答案是基于koma的,但它不需要覆盖onStart,但只需要onCreateView,当你创建新的片段时,默认情况下几乎总是被覆盖。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container);
Rect displayRectangle = new Rect();
Window window = getDialog().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
v.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
v.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
return v;
}
我在Android 5.0.1上进行了测试。