2013-03-23 126 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(...)可能正是你正在尋找的。