我有DialogFragment
我在onCreate()中创建了alertDialog
:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
if (alertDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
}
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.setView(getDialogLayout(),0,0,0,0);
return alertDialog;
}
然后我在alertDialog
中设置onStart()
的宽度(dialogWidth):
@Override
public void onStart() {
super.onStart();
WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
lp.width = dialogWidth;
lp.x = Constants.iX_PositionDialog;
lp.y = Constants.iY_PositionDialog;
alertDialog.getWindow().setAttributes(lp);
}
在我的情况下,我将对话框的宽度设置为648但是我的surfaceView的画布/窗口只有590,为什么? 我需要设置的宽度。
答案 0 :(得分:0)
在show()
的{{1}}方法之后设置布局。
alertDialog
注意:在AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(648, 400); //Controlling width and height.
之后设置布局是关键点。
更多信息 - how-to-control-the-width-and-height-of-default-alert-dialog-in-android.
答案 1 :(得分:0)
设置警报屏幕的宽度和高度以供使用屏幕:
int dialogWidth = getActivity().getResources().getDisplayMetrics().widthPixels-120; // screen width - whatever the width you want to set.
int dialogHeight = getActivity().getResources().getDisplayMetrics().heightPixels -140; //screen height - whatever the width you want to set.
getDialog().setCanceledOnTouchOutside(false);
Window window = getDialog().getWindow();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.gravity = Gravity.CENTER;
lp.height = dialogHeight;
lp.width = dialogWidth;
window.setAttributes(lp);
它是一种最佳实践,因为在andorid中有许多设备和许多分辨率,所以我们必须根据不同的屏幕进行每一次。所以它对所有类型的屏幕都是可行的。
答案 2 :(得分:0)
我为自己找到了解决方案。
因此,alertDialog View包含在几个FrameLayouts中。
其中一些的填充不是0.
根据此帮助代码:[AlertDialog with custom view: Resize to wrap the view's content
我使用以下在onStart方法中调用的方法来实现:
protected void forceWrapContent(View v) {
// Start with the provided view
View current = v;
// Travel up the tree until fail, modifying the LayoutParams
do {
// Get the parent
ViewParent parent = current.getParent();
// Check if the parent exists
if (parent != null) {
// Get the view
try {
current = (View) parent;
} catch (ClassCastException e) {
// This will happen when at the top view, it cannot be cast to a View
break;
}
// Modify the layout
current.getLayoutParams().width = dialogWidth;
current.setPadding(0, 0, 0, 0);
}
} while (current.getParent() != null);
// Request a layout to be re-done
current.requestLayout();
}
谢谢!