2012-11-10 6 views
1

onCreate 내에서 statusBar 높이를 얻으려고하고 있지만 여기에서 발견 한 메서드는 이미 크기를 가져 와서 statusBar 높이를 계산해야하는 뷰가 필요합니다. 나는에서 onCreate에있어 이후onCreate() 내 statusBar 높이 가져 오기

, 내가 그것을 크기가 여기에 나를 도울 수

사람의 취득을 위해 아직 아무것도 그릴 수 없습니다 거기에?

답변

1

사용 글로벌 레이아웃 리스너

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is 
    LinearLayout mainLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.main, null); 

    // set a global layout listener which will be called when the layout pass is completed and the view is drawn 
    mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() { 
      public void onGlobalLayout() { 
       // measure your views here 
      } 
    } 
); 

setContentView(mainLayout); 

[편집]

한 번만이 작업을 수행하려면 i는 측정 방법을 호출 할 수있는 다른 방법은

ViewTreeObserver observer = mainLayout.getViewTreeObserver(); 

observer.addOnGlobalLayoutListener (new OnGlobalLayoutListener() { 
@Override 
public void onGlobalLayout() { 
    // measure your views here 
    mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    } 
}); 
+0

있다 한 번만 (앱 시작시) –

+0

위와 같이 수신기를 설정 한 다음 onGlobalLayout()에서 제거하십시오! Android 설명서를 읽는 습관을 갖도록 노력하십시오. http://developer.android.com/reference/android/view/ViewTreeObserver.html#removeOnGlobalLayoutListener(android.view.ViewTreeObserver.OnGlobalLayoutListener) – Simon

+0

내 답변을 수정했습니다. – Simon

1
root = (ViewGroup)findViewById(R.id.root); 
root.post(new Runnable() { 
public void run(){ 
    Rect rect = new Rect(); 
    Window win = getWindow(); 
    win.getDecorView().getWindowVisibleDisplayFrame(rect); 
    int statusHeight = rect.top; 
    int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 
    int titleHeight = contentViewTop - statusHeight; 
    Log.e("dimen", "title = " + titleHeight + " status bar = " + statusHeight); 
} 
});