2011-09-24 79 views
0

我有一個java文件,它的setContentView是另一個Java文件...的Android的Java添加一個TextView

這是第一個Java文件

package dalton.metzler.occupied; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.TextView; 


public class PlayActivity extends Activity{ 

    Play ourView; 

    LinearLayout linear; 
    TextView text; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     ourView = new Play(this); 
     setContentView(ourView); 
    } 



} 

這裏是多數民衆贊成文件鏈接到它作爲設置contentview沒有你需要看只顯示的特殊代碼,所以你可以看到它是什麼。

,看看到底是什麼。我是指文本的東西我試圖做

package dalton.metzler.occupied; 

import java.util.Random; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.view.View; 


    public class Play extends View { 

    Bitmap gBall1, gBall2, gBall3, gBall4, gBall5, gBall6, gBall7, gBall8, gBall9, gBall10, gBall11, gBall12, gBall13; 

    float changingY, changingY2, changingY3, changingY4, changingY5, changingY6, changingY7, changingY8, changingY9, changingY10, changingY11, changingY12, changingY13; 


    public Play(Context context) { 
     super(context); 


     gBall1 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans); 
     gBall2 = BitmapFactory.decodeResource(getResources(), R.drawable.womentrans); 
     gBall3 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans); 
     gBall4 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans); 
     changingY = 0; 
     changingY2 = -110; 
     changingY3 = -220; 
     changingY4 = -330; 
     changingY5 = -440; 
     changingY6 = -550; 
     changingY7 = -660; 
     changingY8 = -770; 
     changingY9 = -880; 
     changingY10 = -990; 
     changingY11 = -1100; 
     changingY12 = -1210; 
     changingY13 = -1320; 

    } 

    protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawColor(Color.WHITE); 
    canvas.drawBitmap(gBall1, (canvas.getWidth()/2), changingY, null); 
    canvas.drawBitmap(gBall2, (canvas.getWidth()/2), changingY2, null); 
    canvas.drawBitmap(gBall3, (canvas.getWidth()/2), changingY3, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY4, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY5, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY6, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY7, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY8, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY9, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY10, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY11, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY12, null); 
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY13, null); 
    if (changingY < canvas.getHeight()){ 
     changingY += 5; 

    }else{ 
     changingY = -600; 
    } 
    if (changingY2 < canvas.getHeight()){ 
     changingY2 += 5; 

    }else{ 
     changingY2 = -600; 
    } 
    if (changingY3 < canvas.getHeight()){ 
     changingY3 += 5; 

    }else{ 
     changingY3 = -600; 
    } 
    if (changingY4 < canvas.getHeight()){ 
     changingY4 += 5; 

    }else{ 
     changingY4 = -600; 
    } 
    if (changingY5 < canvas.getHeight()){ 
     changingY5 += 5; 

    }else{ 
     changingY5 = -600; 
    } 
    if (changingY6 < canvas.getHeight()){ 
     changingY6 += 5; 

    }else{ 
     changingY6 = -600; 
    } 
    if (changingY7 < canvas.getHeight()){ 
     changingY7 += 5; 

    }else{ 
     changingY7 = -600; 
    } 
    if (changingY8 < canvas.getHeight()){ 
     changingY8 += 5; 

    }else{ 
     changingY8 = -600; 
    } 
    if (changingY9 < canvas.getHeight()){ 
     changingY9 += 5; 

    }else{ 
     changingY9 = -600; 
    } 
    if (changingY10 < canvas.getHeight()){ 
     changingY10 += 5; 

    }else{ 
     changingY10 = -600; 
    } 
    if (changingY11 < canvas.getHeight()){ 
     changingY11 += 5; 

    }else{ 
     changingY11 = -600; 
    } 
    if (changingY12 < canvas.getHeight()){ 
     changingY12 += 5; 

    }else{ 
     changingY12 = -600; 
    } 
    if (changingY13 < canvas.getHeight()){ 
     changingY13 += 5; 

    }else{ 
     changingY13 = -600; 
    } 
    invalidate(); 
    }  
} 

我如何添加文本應用程序?像一個textview,但不是在XML中,因爲我不能這樣做(我認爲) 我想添加像文本:得分:0到屏幕

+0

您應該使用數組而不是維護長變量列表。這種方法允許您使用for循環來操作數組中的所有元素。 – Merlin

+0

我該如何做到這一點? –

+0

你需要學習編程的基礎知識。有一個很好的[數組教程](http://mobile.tutsplus.com/tutorials/android/java-arrays/)一個tutsplus。你應該加快閱讀所有的章節,以便你知道什麼是可能的,然後在你的程序中使用它的時候研究每一章。瞭解正確的技術將幫助您編寫高效簡潔的代碼。祝你好運,) – Merlin

回答

0

現在你不能添加另一個視圖到你的自定義視圖爲它擴展了View類。您需要擴展viewgroup類以添加其他視圖。

創建線性佈局並添加播放視圖和文本視圖,並在setContentView函數中設置線性佈局。

如果你只是需要在playview上繪製文本,那就是canvas.drawText函數。

我認爲你的drawText方法是錯誤的。嘗試這個。

Paint orangePaint = new Paint(); 
    orangePaint.setColor(Color.BLACK); 

    canvas.drawText("test", 50, 50, orangePaint); 

這應該有效。

+0

嗨,我做了這個canvas.drawText(「test」,50,50,Color.BLACK,0,null);在我的保護無效onDraw(帆布畫布){方法和我有一個力量關閉 –

+0

更新你的問題與堆棧跟蹤。讓我們看看最新的錯誤。 – blessenm

+0

堆棧跟蹤是什麼? –

相關問題