2011-07-25 29 views
0

我知道很多人都有過類似的問題,這一點,但我不能找到一個解決我的自定義視圖是不可見的。我創建了一個擴展SurfaceView的自定義視圖。當我使用這個視圖完美的作品:嵌入裏面的時候Horizo​​ntalScrollView

setContentView(graphView); 

但是我希望它被嵌入在Horizo​​ntalScrollView內。如果我用xml試試這個:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<HorizontalScrollView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:fillViewport="true"> 

<com.stuff.graph.GraphView 
android:id="@+id/graph" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

</HorizontalScrollView> 
</FrameLayout> 

然後我得到一個致命的異常,我的應用程序關閉。

如果我嘗試使用編程解決這個問題:

graphView = new GraphView(this); 
graphView.setVisibility(View.VISIBLE); 
myScrollView = new HorizontalScrollView(this); 

graphView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   

myScrollView.addView(graphView);  

setContentView(myScrollView); 

這時屏幕只是黑色的。我也在上面顯示的代碼上嘗試了很多變體,但都沒有成功。我哪裏錯了?

+0

你的GraphView擴展了什麼.. – ngesh

+0

它擴展了SurfaceView,實現了SurfaceHolder.Callback – user650309

回答

0

你應該能夠得到它在XML沒有問題 - 嘗試嵌入滾動視圖內一個ListView裏面你的自定義圖表視圖。在我使用horizo​​ntalScrollView的經驗中,他們傾向於選擇他們的內容。如果這不起作用,請嘗試測試以查看幀視圖是否有問題。只需在水平滾動視圖中添加一個按鈕,看看是否可以顯示出來。如果沒有,那麼嘗試顯示框架時會出現問題,並且與您的自定義代碼無關。

+0

我沒有嘗試過,但是如果我嘗試使用xml進行佈局,我的應用程序只是崩潰了我的adb logcat,因爲它是:E/AndroidRuntime(2900 ):致命例外:main E/AndroidRuntime(2900):java.lang.RuntimeException: 無法啓動活動ComponentInfo {com.stuff.graph/com.stuff.graph.GraphActivity}: android.view.InflateException:Binary XML文件行#20:錯誤充氣類com.stuff.graph.GraphView – user650309

+0

嗯,好吧,這就是爲什麼你不能充氣水平滾動視圖,它只是垂死更多默默對你。檢查你的GraphView,看看爲什麼android無法膨脹它。此外,你知道android有它自己的graphView類可能會做類似的事情,這個問題可能會像重命名你的類或使用android的一樣簡單。 – Noah

+0

我擺弄約地段與該XML但是沒有什麼工作,在這一點上,我寧願只是把它從Java顯示。你有什麼想法爲什麼它可能無法正確顯示? – user650309

相關問題