Android获取状态栏和标题栏的高度

1.获取状态栏高度:

decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。
于是,我们就可以算出状态栏的高度了。

view plaincopy to clipboardprint?
01.Rect frame = new Rect();  
02.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
03.int statusBarHeight = frame.top; 
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;

2.获取标题栏高度:

getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。

view plaincopy to clipboardprint?
01.int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
02.//statusBarHeight是上面所求的状态栏的高度  
03.int titleBarHeight = contentTop - statusBarHeight 
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight

例子代码:

view plaincopy to clipboardprint?
01.package com.cn.lhq;  
02.import android.app.Activity;  
03.import android.graphics.Rect;  
04.import android.os.Bundle;  
05.import android.util.Log;  
06.import android.view.Window;  
07.import android.widget.ImageView;  
08.public class Main extends Activity {  
09.    ImageView iv;  
10.    @Override 
11.    public void onCreate(Bundle savedInstanceState) {  
12.        super.onCreate(savedInstanceState);  
13.        setContentView(R.layout.main);  
14.        iv = (ImageView) this.findViewById(R.id.ImageView01);  
15.        iv.post(new Runnable() {  
16.            public void run() {  
17.                viewInited();  
18.            }  
19.        });  
20.        Log.v("test", "== ok ==");  
21.    }  
22.    private void viewInited() {  
23.        Rect rect = new Rect();  
24.        Window window = getWindow();  
25.        iv.getWindowVisibleDisplayFrame(rect);  
26.        int statusBarHeight = rect.top;  
27.        int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)  
28.                .getTop();  
29.        int titleBarHeight = contentViewTop - statusBarHeight;  
30.        // 测试结果:ok之后 100多 ms 才运行了  
31.        Log.v("test", "=-init-= statusBarHeight=" + statusBarHeight  
32.                + " contentViewTop=" + contentViewTop + " titleBarHeight=" 
33.                + titleBarHeight);  
34.    }  
35.} 
package com.cn.lhq;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
public class Main extends Activity {
 ImageView iv;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  iv = (ImageView) this.findViewById(R.id.ImageView01);
  iv.post(new Runnable() {
   public void run() {
    viewInited();
   }
  });
  Log.v("test", "== ok ==");
 }
 private void viewInited() {
  Rect rect = new Rect();
  Window window = getWindow();
  iv.getWindowVisibleDisplayFrame(rect);
  int statusBarHeight = rect.top;
  int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)
    .getTop();
  int titleBarHeight = contentViewTop - statusBarHeight;
  // 测试结果:ok之后 100多 ms 才运行了
  Log.v("test", "=-init-= statusBarHeight=" + statusBarHeight
    + " contentViewTop=" + contentViewTop + " titleBarHeight="
    + titleBarHeight);
 }
}

view plaincopy to clipboardprint?
01. 
02. 03.    android:orientation="vertical" 
04.    android:layout_width="fill_parent" 
05.    android:layout_height="fill_parent"> 
06.    07.        android:id="@+id/ImageView01"   
08.        android:layout_width="wrap_content"   
09.        android:layout_height="wrap_content"/> 
10.
 


http://blog.csdn.net/pilou5400/archive/2010/11/18/6018422.aspx

你可能感兴趣的:(Android获取状态栏和标题栏的高度)