在自定义活动中绘制 TextView?
我正在实现一个自定义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
来代替?我不确定那会如何运作。
I'm implementing a custom View
, and I need to draw some text in it. The text has to fit in a box (so I have to break it up and make it fit). Because of this, I thought I could use a TextView
and draw it inside my custom View
. Here's what I've tried:
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();
However, nothing is showing up. I know rect
is OK because the first drawRoundRect
works fine. What am I missing? Is there a better way? Maybe I should extend ViewGroup
instead? I'm not sure how that would work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在不包装的情况下执行此操作:
通过包装
http://developer.android 来执行此操作。 com/reference/android/text/StaticLayout.html
To do it without wrapping:
to do it with wrapping
http://developer.android.com/reference/android/text/StaticLayout.html
我当前的解决方案是这样的:
感觉很脏(而且有些不理想),但它有效。如果有人没有提出更好的解决方案,我将在几天内将其标记为已接受。
My current solution is this:
It feels dirty (and somewhat un-optimal), but it works. If someone doesn't come up with a better solution I'm going to mark this as accepted in a couple of days.