我目前正在開發一款適用於Android的簡單遊戲,其中的主要操作發生在我的GameView類上,該類只是View的擴展。將TextView添加到遊戲主視圖的最佳方式是什麼?
我想添加一些文字來顯示我的GameView的左上角的分數,並想知道實現這一點的最佳方式?
在此先感謝
馬特Drewery
我目前正在開發一款適用於Android的簡單遊戲,其中的主要操作發生在我的GameView類上,該類只是View的擴展。將TextView添加到遊戲主視圖的最佳方式是什麼?
我想添加一些文字來顯示我的GameView的左上角的分數,並想知道實現這一點的最佳方式?
在此先感謝
馬特Drewery
我假設你GameView有帆布,因爲它繪製圖形,對不對?在這種情況下,當你做你的繪圖的其餘部分,使用
//canvas.drawText(String text, float x, float y, Paint paint); this is the actual method
canvas.drawText(score, 5, 5, null); //but yours would look more like this
那麼你就只需要創建的「分數」的字符串,並把它通過遊戲進行更新。
此外,你可能想嘗試addView(查看孩子)。它應該是這樣的:
GameView.addView(TextView);
有點無恥的自我宣傳在這裏,但如果你偷看Hexaddicus你會看到,我們分割畫面到遊戲視圖和頁腳是純老視的東西。這我不是你要去的「外觀」,但它是a的方式。如果你想要更內聯的外觀和感覺,那麼顯然你不應該走這條路。
只是不要嘗試覆蓋Android遊戲視圖的Android UI小部件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/control_pane"
android:layout_width="fill_parent"
android:layout_height="48dip"
android:layout_alignParentBottom="true">
</FrameLayout>
<com.sharpermindstech.android.hexaddicus.opengl.views.HexBoardView
android:id="@+id/gamefield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/control_pane" />
</RelativeLayout>