02 | getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); ///取得整个视图部分,注意,如果你要设置标题样式,这个必须出现在标题样式之后,否则会出错 |
03 | int
top = rect.top; ////状态栏的高度,所以rect.height,rect.width分别是系统的高度的宽度 |
04 | View
v = getWindow().findViewById(Window.ID_ANDROID_CONTENT); ///获得根视图 |
05 | int
top2 = v.getTop(); ///状态栏标题栏的总高度,所以标题栏的高度为top2-top |
06 | int
width = v.getWidth(); ///视图的宽度,这个宽度好像总是最大的那个 |
07 | int
height = v.getHeight(); ////视图的高度,不包括状态栏和标题栏 |
10 | Display
display = getWindowManager().getDefaultDisplay() ; |
15 | 在onCreate中不能得到,要在onWindowFocusChanged中才能得到: |
18 | public
class MainActivity extends
Activity { |
21 | /**
Called when the activity is first created. */ |
23 | public
void onCreate(Bundle savedInstanceState) { |
24 | super .onCreate(savedInstanceState); |
25 | setContentView(R.layout.main); |
31 | public
void onWindowFocusChanged( boolean
hasFocus) { |
32 | //
TODO Auto-generated method stub |
33 | super .onWindowFocusChanged(hasFocus); |
35 | getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); |
36 | int
statusBarHeight = frame.top; |
39 | int
contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
|
40 | //statusBarHeight是上面所求的状态栏的高度 |
41 | int
titleBarHeight = contentTop - statusBarHeight ; |
43 | textView
= (TextView)findViewById(R.id.textView1); |
44 | textView.setText( "状态栏的高度"
+ Integer.toString(titleBarHeight)); |
|
横屏.png (38.2
KB, 下载次数: 0)
竖屏.png (48.77
KB, 下载次数: 0)
图形源码.png (20.56
KB, 下载次数: 0)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理