Android: Adding Background on a Custom TextView class

后端 未结 3 707
陌清茗
陌清茗 2020-12-20 00:04

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 cu

相关标签:
3条回答
  • 2020-12-20 00:39

    Try setBackground or setBackgroundResource

    0 讨论(0)
  • 2020-12-20 00:58

    See Scale Drawable and Shape Drawable

    0 讨论(0)
  • 2020-12-20 01:01

    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.

    0 讨论(0)
提交回复
热议问题