2013-03-23 116 views
0

你好,我在我的画布中添加了矩形,现在我想添加textview或其他一些视图在那个矩形。也给我一些教程。在此先感谢以编程方式添加矩形

public class DrawView extends View { 

    public DrawView(Context context) { 
     super(context); 
    } 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Rect rect = new Rect(); 
     rect.set(20 ,10 ,canvas.getWidth()/2, canvas.getHeight()/2); 
     Paint paint = new Paint(); 
     paint.setColor(Color.GREEN); 
     canvas.drawRect(rect, paint); 
    } 
} 
+0

使用canvas.drawText(文字,X,Y,油漆)快照 – Raghunandan 2013-03-23 11:19:32

+0

@Raghunandan它是不可见 – OmD 2013-03-23 11:27:42

+0

我不明白您的评论。不可见? – Raghunandan 2013-03-23 11:28:07

回答

0

您可以在画布上绘制文字。

 Paint mpaint= new Paint(); 
     mpaint.setColor(Color.RED);//set red color for rectangle 
     mpaint.setStyle(Paint.Style.FILL);//mpaint will fill the rectangle 
     Paint paint2 = new Paint(); 
     paint2.setColor(Color.GREEN);//green color for text 
     paint2.setTextSize(30f);//set text size. you can change the stroke width also 

@Override 
protected void onDraw(Canvas canvas) 
    { 
    canvas.drawRect(30, 30, 600, 600, mpaint); 
    canvas.drawText("hello", 150, 150, paint2);//change x and y according to your needs 
    } 

所得的三星Galaxy S3 enter image description here

0

添加任何类型的视图在这里都不成问题,因为您只是在画布上绘图。但Canvas.drawText(...)可能正是你正在寻找的。