簡體   English   中英

對話框未設置布局參數

[英]dialog not setting layout params

使用對話框作為窗口,我試圖將其布局設置為

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);

在按鈕單擊內,但未設置它。

我想你應該用這個

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

這將設置對話框窗口的大小。

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>

根據文檔: http : //developer.android.com/reference/android/view/WindowManager.LayoutParams.html

  1. 此窗口的X位置。 使用默認重力,將忽略它。 當使用LEFT或START或RIGHT或END時,它將提供給定邊的偏移量。

  2. 該窗口的Y位置。 使用默認重力,將忽略它。 當使用TOP或BOTTOM時,它提供給定邊的偏移量。

因此,要使x和y起作用,您應該先設置Gravity屬性。 例如:

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

//對話框的寬度和高度是根據屏幕設置的,可以隨心所欲

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM