安卓:在自定义的TextView类添加背景 [英] Android: Adding Background on a Custom TextView class

查看:608
本文介绍了安卓:在自定义的TextView类添加背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有我的TextView类的背景,但我不知道怎么办。我尝试使用的OnDraw方法的类,但它不能正常工作。

I want to have a background of my TextView class, but I don't know how. I try to use the onDraw method on the class but it is not working.

下面是我的code为我定制的TextView类。

Here is my code for my custom TextView class.

public class Balls extends TextView{

public Balls(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.setText("ball");
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    canvas.drawCircle(50, 50,30, paint);
}}

任何想法,我怎么能解决这个问题code?谢谢你。

Any idea how can I fix this code? Thanks.

推荐答案

您可以按以下方式设置背景,

You can set Background in following way,

public class Balls extends TextView{

    public Balls(Context context) {
        super(context);
        this.setText("ball");
        this.setBackgroundColor(R.drawable.imageName);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(50, 50,30, paint);
    }
}

我用

 this.setBackgroundColor(R.drawable.imageName);

在构造函数来设置背景Image.Also您可以在同样的方法设置背景色。

in constructor to set Background Image.Also you can set background color in same way.

这篇关于安卓:在自定义的TextView类添加背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆