1

I have an activity with

android:theme="@android:style/Theme.Dialog" 

to show it as float window. Also I have a line

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

And I need a width/height of this window to show content correctly.

I've tested four options and did not get an answer:

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

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

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

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

Thanks!

4

1 回答 1

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

这将返回屏幕宽度的值

于 2012-06-25T23:05:47.237 回答