2012-06-10 36 views
0

我只是想計算一些在onNewIntend(Intent intent)方法中繪製的線位置。鑑於一些緯度/經度,我必須將它們融入到自定義視圖中。 對於這種計算 - 這是我的問題 - 我需要myView.getHeight()/ getWidth()方法爲0,因爲onNewIntend(...)在onCreate(...)之前被調用。 :/Android:OnNewIntent在膨脹之前需要自定義視圖的大小

你有什麼想法來解決這個問題嗎?

我已經嘗試過...

@Override 
public void onNewIntent(Intent intent) { 
    final String action = intent.getAction(); 

    if (Intent.ACTION_RUN.equals(action)) {   

     // @Todo: Here should be checked if view is already inflated ... 
     setContentView(R.layout.collector); 
     int x = mGraphView.getMeasuredHeight(); 
     ... 

...這收益率X = 0,也

非常感謝!

回答

0

覆蓋自定義視圖中的onMeasure,只要視圖父節點要求其測量自身,就可以獲取寬度和高度。您可以添加一個佈局聽衆

http://developer.android.com/reference/android/view/View.OnLayoutChangeListener.html

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth/2, parentHeight); 
    this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight)); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

}