如何使用FILL_PARENT布局获得Dialog的宽度/高度?

时间:2012-06-25 22:45:10

标签: android android-layout

我有

的活动
android:theme="@android:style/Theme.Dialog" 

将其显示为浮动窗口。我也有一条线

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

我需要此窗口的宽度/高度才能正确显示内容。

我测试了四个选项并没有得到答案:

//1 
int width = getWindowManager().getDefaultDisplay().getWidth();

//2 
int width = getWindow().getDecorView().getWidth();

//3 
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

//4 
int width = getWindow().getAttributes().width;

谢谢!

1 个答案:

答案 0 :(得分:2)

使用此功能:

private int getting_screen_width(Dialog dialog) 
    { 
    DisplayMetrics dm = new DisplayMetrics();
     dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    String width = ""+dm.widthPixels ;
    dm = null;
       return (Integer.parseInt(width));

这会返回屏幕宽度的值