Android java添加一个textView

时间:2011-09-24 03:43:03

标签: java android

我有一个java文件,其setContentView是另一个java文件......

这是第一个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);
    }



}

以下是链接到它的文件作为设置内容视图,您不需要查看显示的特殊代码,这样您就可以看到它是什么。

确切地看到我对我想要做的文字的意思

    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到屏幕

1 个答案:

答案 0 :(得分:0)

现在,您无法在自定义视图中添加另一个视图,因为它扩展了View类。您需要扩展视图组类以添加其他视图。

创建线性布局并将播放视图和textview添加到该布局并在setContentView函数中设置线性布局。

如果你只需要在playview上绘制文字,那就是canvas.drawText函数。

我认为你的drawText方法是错误的。试试这个。

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

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

这应该有用。