android取得系统高度,标题栏和状态高度

来源:互联网 发布:淘宝正品运动鞋店铺 编辑:程序博客网 时间:2024/05/21 22:35

01Rect rect = newRect();
02getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);///取得整个视图部分,注意,如果你要设置标题样式,这个必须出现在标题样式之后,否则会出错
03inttop = rect.top;////状态栏的高度,所以rect.height,rect.width分别是系统的高度的宽度
04View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);///获得根视图
05inttop2 = v.getTop();///状态栏标题栏的总高度,所以标题栏的高度为top2-top
06intwidth = v.getWidth();///视图的宽度,这个宽度好像总是最大的那个
07intheight = v.getHeight();////视图的高度,不包括状态栏和标题栏
08 
09如果只想取得屏幕大小,可以用
10Display display = getWindowManager().getDefaultDisplay() ;
11display.getWidth();
12display.getHeight();
13 
14 
15在onCreate中不能得到,要在onWindowFocusChanged中才能得到:
16 
17 
18publicclass MainActivity extendsActivity {
19TextView textView;
20 
21/** Called when the activity is first created. */
22@Override
23publicvoid onCreate(Bundle savedInstanceState) {
24super.onCreate(savedInstanceState);
25setContentView(R.layout.main);
26 
27 
28}
29 
30@Override
31publicvoid onWindowFocusChanged(booleanhasFocus) {
32// TODO Auto-generated method stub
33super.onWindowFocusChanged(hasFocus);
34Rect frame = newRect();
35getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
36intstatusBarHeight = frame.top;
37 
38 
39intcontentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
40//statusBarHeight是上面所求的状态栏的高度
41inttitleBarHeight = contentTop - statusBarHeight ;
42 
43textView = (TextView)findViewById(R.id.textView1);
44textView.setText("状态栏的高度"+ Integer.toString(titleBarHeight));
45}
46 
47 
48 
49}

横屏.png (38.2 KB, 下载次数: 0)

横屏.png

竖屏.png (48.77 KB, 下载次数: 0)

竖屏.png

图形源码.png (20.56 KB, 下载次数: 0)

图形源码.png