2011-07-25 53 views
0

我有我把滾動型像這樣的自定義視圖:Android的滾動型隱形自定義視圖

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <com.test.view android:id="@+id/myview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</ScrollView> 

我已經實現的onDraw(),並在我的自定義視圖許多其他的方法,也onMeasure:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(600, MeasureSpec.EXACTLY)); 
     width = MeasureSpec.getSize(widthMeasureSpec); 
     height = MeasureSpec.getSize(heightMeasureSpec); 
} 

然後當我啓動應用程序時,我從onDraw()方法中獲取了很多日誌,TouchEvent沒有問題,但是我看不到我的自定義View。它似乎在那裏,但好像它是無形的...

也許我錯過了什麼......?

感謝

UPDATE

自定義視圖是沒什麼特別的,只是一個視圖,顯示位圖:

private class myView extends View{ 

public myView(Context context) { 
    super(context); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.packagebm); 
    canvas.drawBitmap(bm, 0, 0, null); 
    Log.i("myview", "onDraw()"); 
} 

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY)); 

    } 
    } 
+0

它應該是什麼樣子?也許從你的視圖 – FoamyGuy

+0

張貼完整的代碼,如果你擺脫scrollview是否有所作爲? – FoamyGuy

+0

<_ <是的,因爲我希望視圖在ScrollView .. – Ceeds

回答

0

你要叫setMeasuredDimension在自定義視圖的onMeasure。所以它應該看起來像這樣:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, height); 
    setMeasuredDimension(widthMeasureSpec, resolveSize(600, heightMeasureSpec)); 
} 
+0

萬一如果我把自定義視圖在scrollView它只是onSizeChanged()和onDraw()沒有呼籲查看...任何建議? – CoDe