2011-08-06 23 views
1

我正在創建一個android遊戲,我嘗試使用canvas.drawText()方法來顯示我的分數和級別,但是它導致了錯誤。現在我正在嘗試使用TextViews來顯示它們,我使用的是SurfaceVeiw,並且我想知道它是否可行,並且它會是一個很好的方法。SurfaceView中的多個TextViews

回答

3

你不能真正把TextViews放入SurfaceView。相反,你可以做的是在SurfaceView上添加TextViews。如果你不知道如何,如果你需要幫助,請告訴我。

// You can use a FrameLayout to hold the surface view 
FrameLayout frameLayout = new FrameLayout(this); 
frameLayout.addView(surfaceView); 

// Then create a layout to hold everything, for example a RelativeLayout 
RelativeLayout relativeLayout= new RelativeLayout(this); 
relativeLayout.addView(frameLayout); 
relativeLayout.addView(textview1); 
relativeLayout.addView(textview2); 
setContentView(relativeLayout); 

添加視圖時,使用LayoutParams幫助組織事情可能會有幫助。 LayoutParams使用此documentation。希望這可以幫助!

如果您使用的是XML佈局,我將使用RelativeLayout(如上所述)在FrameLayout中包含SurfaceView。將TextView放置在FrameLayout之上只是爲每個視圖配置RelativeLayout參數的一個簡單問題。您似乎對Android很陌生,或許這guide將幫助您使用RelativeLayouts和XML佈局。

+0

如果你能幫上忙,那麼我該如何在上面添加TextView。 – Manji

+0

我的xml文件應該如何添加,因爲我將它設置爲JetBoy&Lunar Lander如何設置它們的xml文件。 – Manji