我有一个相对布局R和一个alertdialog A.我想像这样设置A的位置和尺寸与R相同:
A.position_x = R.get_position_x()
A.position_y = R.get_position_y()
A.width = R.get_width()
A.height = R.get_height()
答案 0 :(得分:1)
在屏幕顶部使用以下代码设置AlertDialog
:
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Title:");
builder.setMessage("Are you sure to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Set ypu positive code on OK button
finish();
}
});
// Setting Negative "NO" Btn
builder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Dialog
AlertDialog dialog = builder.create();
dialog.getWindow().setGravity(Gravity.TOP);
dialog.show();
答案 1 :(得分:0)
从代码下方获取relativelayout的高度,宽度和位置x和y
rel1 = (RelativeLayout)findViewById(R.id.rel1);
rel1.post(new Runnable()
{
@Override
public void run()
{
widthRel1 = rel1.getWidth();
heightRel1 = rel1.getHeight();
xRel1 = (int) rel1.getX();
yRel1 = (int) rel1.getY();
}
});
然后将其应用于Dialog
final Dialog alertDialog = new Dialog(MainActivity.this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.template_menu);
Window window = alertDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.x = xRel1; //x position
wlp.y = yRel1; //y position
wlp.height = heightRel1;
wlp.width = widthRel1;
wlp.gravity = Gravity.TOP | Gravity. LEFT;
window.setAttributes(wlp);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.show();