在Android中显示多行文本

发布于 2024-10-14 08:44:25 字数 294 浏览 3 评论 0原文

我有一个图形化 Android 项目,提供用户界面功能的主要技巧是使用drawrect 和drawtext 在屏幕上绘制一个带有标签的矩形。然后,我捕获触摸事件并检查它们是否发生在矩形中 - 当它们发生时,瞧,我有一个按钮。

这可能不是最优雅的方法,但似乎很有魅力。然而,并不是我的所有标签都在一行上看起来很好,我想将它们写在两行上。我想我可以写两行单独的行,然后手动将文本排列成很好的间距和居中位置,但如果可能的话,我想避免这种情况。

在Android中,有没有一种简单的方法可以分割文本标签并一步将其写出来?

谢谢, R。

I have a graphical Android project and my primary trick for providing user interface functionality is to use drawrect and drawtext to draw a rectangle with a label on the screen. Then, I capture touch events and check to see if they occur in the rectangle -- when they do, voila, I have a button.

It may not be the most elegant method, but is seems to be working like a charm. However, not all of my labels look good all on one line, and I'd like to write them on two. I suppose I could write two separate lines and manually work out arranging the text to be nicely spaced and centered, but I'd like to avoid that if possible.

In Android, is there a simple way to split a text label and write it out in a single step??

Thanks,
R.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-10-21 08:44:25

我认为没有一个简单的方法可以实现这一点,您必须测量文本并使用 ButtonWidth - TextWidth / 2 的偏移量来绘制它。

I don't think there is a easy way to achieve this, you will have to measure the text and draw it with an offset that is ButtonWidth - TextWidth / 2.

等待我真够勒 2024-10-21 08:44:25

在 Android 应用程序中,您可以创建布局并使用各种小部件填充它。这些潜在的小部件之一是按钮,如果您使用它,如果标签的长度需要,它将在多行上显示按钮标签...

但是,我的应用程序是 SurfaceRunner,并且此视图不支持按钮,据我所知。我确实通过绘制各种矩形并检查其边界内的屏幕触摸来使用按钮式功能。这对于我的目的来说效果很好,但无论如何都不会自动执行任何显示任务(就像按钮小部件一样)。为了在两行上显示长标签,有必要在代码中显式地执行此操作。那好吧。

幸运的是,它只是额外的一行:

canvas.drawRoundRect (Button, ROUNDOVER_SIZE-1, ROUNDOVER_SIZE-1, paint);
canvas.drawText (Text1, Button.centerX() - paint.measureText (Text1)/2, Button.centerY() - 10, paint);
canvas.drawText (Text2, Button.centerX() - paint.measureText (Text2)/2, Button.centerY() + 20, paint);

显然,如果我使用比“10”和“20”更自动的东西会更酷,但这确实完成了工作。

In an Android application, you can create a Layout and populate it with a variety of widgets. One of these potential widgets is the button and if you use it, it WILL display your button label on more than one line if the label's length requires that...

However, my application is a SurfaceRunner, and this view doesn't support buttons, as far as I can tell. I do use a button-ish functionality by drawing various rectangles and checking for screen touches inside their boundaries. This works fine for my purposes, but does not, in any way, automate any of the display tasks (like the button widget does). In order to display a long label on two lines, it's necessary to do that explicitly in the code. Oh well.

Fortunately, it's only one additional line:

canvas.drawRoundRect (Button, ROUNDOVER_SIZE-1, ROUNDOVER_SIZE-1, paint);
canvas.drawText (Text1, Button.centerX() - paint.measureText (Text1)/2, Button.centerY() - 10, paint);
canvas.drawText (Text2, Button.centerX() - paint.measureText (Text2)/2, Button.centerY() + 20, paint);

Obviously, it would have been cooler if I had used something a tad more automatic than "10" and "20", but this does get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文