开发者

Android java Add a textView

I have a java file that its setContentView is to another java file...

Here is the first java file

    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);
    }



}

And here is the file thats linking to it as the set contentview there is not speical code you need to look at just showing so you can see what it is.

And see exactly what i mean about the text thing i am trying to do

    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{
        c开发者_C百科hangingY7 = -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();   
    }       
}

How can i add text to the app? Like a textview but not in the xml because i can't do this (i think) I want to add like the text: Score: 0 to the screen


Right now you cannot added another view to your custom View as it extends the View class. You will need to extend a viewgroup class to add other views.

Create a linear layout and add the play view and the textview to that and set the linear layout in the setContentView function.

If you just need to draw text on playview, there is the canvas.drawText function.

I think your drawText method is wrong. Try this.

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

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

This should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜