我正在实施一个自定义View
,我需要在其中绘制一些文本。文本必须适合一个盒子(所以我必须将其分解并使其适合)。因此,我想我可以使用TextView
并在我的自定义中绘制它View
。这是我尝试过的:
canvas.drawRoundRect(rect, eventRadius, eventRadius, eventBg);
canvas.save();
canvas.clipRect(rect);
TextView tv = new TextView(getContext());
tv.setText(e.getSummary());
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tv.layout(0, 0, (int) (rect.right - rect.left), (int) (rect.bottom - rect.top));
tv.draw(canvas);
canvas.restore();
然而,什么也没有出现。我知道rect
可以,因为第一个drawRoundRect
工作正常。我缺少什么?有没有更好的办法?也许我应该延长ViewGroup
反而?我不确定那会如何运作。
不包装即可做到这一点:
canvas.drawText(yourText, xCoord,YCoord, YourPaint);
用包装来做
protected void onDraw(Canvas canvas) {
TextPaint tp=new TextPaint();
tp.setARGB(255, 255, 0, 0);
tp.setTextSize(12);
StaticLayout sl=new StaticLayout("THIS IS SOME LONGER TEXT",tp,60,Layout.Alignment.ALIGN_NORMAL,1f,0f,true);
sl.draw(canvas);
}
http://developer.android.com/reference/android/text/StaticLayout.html http://developer.android.com/reference/android/text/StaticLayout.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)