01
Rect rect =
new
Rect();
02
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
03
int
top = rect.top;
04
View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
05
int
top2 = v.getTop();
06
int
width = v.getWidth();
07
int
height = v.getHeight();
08
09
如果只想取得屏幕大小,可以用
10
Display display = getWindowManager().getDefaultDisplay() ;
11
display.getWidth();
12
display.getHeight();
13
14
15
在onCreate中不能得到,要在onWindowFocusChanged中才能得到:
16
17
18
public
class
MainActivity
extends
Activity {
19
TextView textView;
20
21
/** Called when the activity is first created. */
22
@Override
23
public
void
onCreate(Bundle savedInstanceState) {
24
super
.onCreate(savedInstanceState);
25
setContentView(R.layout.main);
26
27
28
}
29
30
@Override
31
public
void
onWindowFocusChanged(
boolean
hasFocus) {
32
33
super
.onWindowFocusChanged(hasFocus);
34
Rect frame =
new
Rect();
35
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
36
int
statusBarHeight = frame.top;
37
38
39
int
contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
40
41
int
titleBarHeight = contentTop - statusBarHeight ;
42
43
textView = (TextView)findViewById(R.id.textView1);
44
textView.setText(
"状态栏的高度"
+ Integer.toString(titleBarHeight));
45
}
46
47
48
49
}