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.
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);
}}
Any idea how can I fix this code? Thanks.
Anil Jadhav
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);
}
}
I used
this.setBackgroundColor(R.drawable.imageName);
in constructor to set Background Image.Also you can set background color in same way.
See Scale Drawable and Shape Drawable
来源:https://stackoverflow.com/questions/12851608/android-adding-background-on-a-custom-textview-class