AlertDialog支持x,y轴定位,但Dialog不在android中

时间:2014-03-12 09:28:06

标签: android alertdialog

我试图为我的对话设置位置。我使用自定义视图,因此我使用Dialog类创建警报对话框。我的问题是,x,y位置不适用于Dialog警报但适用于AlertDialog警报。我不知道为什么会这样。提前感谢...

使用Dialog类的警报对话框代码,此对话框未显示给定位置

Dialog dialog = new Dialog(getActivity(),
            android.R.style.Theme_Translucent_NoTitleBar);
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
    Log.v("ShelfShareFragment", "X-Axis: "+mXAxis+" Y-Axis: "+mYAxis);
    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    dialog.getWindow().setWindowAnimations(R.anim.anim_fade_in);
    wmlp.x = mXAxis; // x position
    wmlp.y = mYAxis; // y position

    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_shelf_share,
            null);

    dialog.setContentView(dialogView);
            dialog.show()

使用AlertDialog类的警报对话框代码。这个工作正常,但我需要使用Dialog类创建我的对话框。

AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_shelf_share,
            null);
    builder.setTitle("Checking");
    builder.setView(dialogView);
    builder.setCancelable(true);
    AlertDialog dialog = builder.create();
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
    Log.v("ShelfShareFragment", "X-Axis: "+mXAxis+" Y-Axis: "+mYAxis);
    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = mXAxis; // x position
    wmlp.y = mYAxis; // y position
    dialog.getWindow().setWindowAnimations(R.anim.anim_zoom_out);
    dialog.show();

1 个答案:

答案 0 :(得分:0)

我认为你错过了代码

getWindow().setAttributes()

请检查