对话框没有设置布局参数

时间:2013-12-27 10:37:09

标签: android android-layout dialog

使用对话框作为窗口我试图将其布局设置为

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

    lp.x = 120;
    lp.y = 120;
    lp.width = 800;
    lp.height = 450;
    Dialog dialog = new Dialog(cxt);
    dialog.getWindow().setAttributes(lp);
    dialog.setContentView(vFirst);
    dialog.show();

其中

View  vFirst = inflater.inflate(R.layout.popup, container, false);

位于按钮点击内,但没有设置它。

4 个答案:

答案 0 :(得分:1)

我认为你应该使用这个

  dialog.getWindow().setLayout(800, 450); 

这将设置对话框窗口大小。

答案 1 :(得分:0)

public class Myclass extends Dialog
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutParams params = getWindow().getAttributes();
        params.height = LayoutParams.FILL_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    }
}

<强>布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/dialogWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   // contents here

</LinearLayout>

答案 2 :(得分:0)

根据文件:http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

  1. 此窗口的X位置。使用默认重力时,忽略。当使用LEFT或START或RIGHT或END时,它提供与给定边缘的偏移。

  2. 此窗口的Y位置。使用默认重力时,忽略。当使用TOP或BOTTOM时,它提供了与给定边缘的偏移。

  3. 因此,要使x和y工作,您应该先设置Gravity属性。例如:

        lp.gravity = Gravity.TOP;
        lp.gravity = Gravity.LEFT;
        lp.y = 120;
        lp.x = 120;
    

答案 3 :(得分:0)

//根据屏幕设置对话框宽度和高度,你可以带你想要

Dialog myDialog = new Dialog(context, R.style.CustomTheme);
            myDialog.setContentView(R.layout.share_post_dialog);

            int[] widthHeight = getDeviceWidthAndHeight(context);
            int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
            int dialogWidth = widthHeight[0] - widthHeight[0] / 5;


        WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
            params.width= dialogWidth;
            params.height = dialogHeight;
            myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);

myDialog.show();

//这用于获取屏幕的高度

@SuppressWarnings("deprecation")
    public static int[] getDeviceWidthAndHeight(Context context) {

        int widthHeght[] = new int[2];
        int measuredWidth = 0;
        int measuredHeight = 0;

        WindowManager w = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

            Point size = new Point();
            w.getDefaultDisplay().getSize(size);
            measuredWidth = size.x;
            measuredHeight = size.y;
            widthHeght[0] = measuredWidth;
            widthHeght[1] = measuredHeight;

        } else {
            Display d = w.getDefaultDisplay();
            measuredWidth = d.getWidth();
            measuredHeight = d.getHeight();
            widthHeght[0] = measuredWidth;
            widthHeght[1] = measuredHeight;

        }
        return widthHeght;
    }