2011-03-09 85 views
16

如何在android ..上畫布上的TextView ..?如何在android ..上畫布上的TextView ..?

我們有Canvas.DrawBitmap()Canvas.drawText()。我們是否在Canvas中有將TextView作爲參數或任何其他方法在Canvas上顯示TextView的方法?

其實,我在TextView中有一個字母表,我必須在畫布上繪製該字母表。

請建議什麼....謝謝您的合作

回答

42

您不能直接繪製Textview,但可以將其放入佈局並繪製佈局。類似這樣的:

LinearLayout layout = new LinearLayout(context); 

TextView textView = new TextView(context); 
textView.setVisibility(View.VISIBLE); 
textView.setText("Hello world"); 
layout.addView(textView); 

layout.measure(canvas.getWidth(), canvas.getHeight()); 
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight()); 

// To place the text view somewhere specific: 
//canvas.translate(0, 0); 

layout.draw(canvas); 
+2

關於measure()調用,我使用MeasureSpec.makeMeasureSpec(canvas.getWidth(),MeasureSpec.EXACTLY)代替canvas.getWidth()(高度相同)。 – 2012-06-12 11:33:50

+0

@emidander非常感謝好友 – Palanivelraghul 2017-10-27 09:53:30

+0

是否可以讓TextView點擊?我添加了一個onClickListener,但它似乎沒有工作。 – user3667569 2018-02-18 19:45:21

0

您需要創建一個擴展Textview的類。之後,重寫onDraw方法。這個方法可以讓你以你喜歡的方式繪製你的textview