android中如何自动调整textSize?

发布于 2024-10-30 18:40:59 字数 123 浏览 1 评论 0原文

我正在编写一个应用程序,其中部分显示一行文本。在某些情况下,该行文本将占用多行。问题是我只希望它占用一行,当我在java或xml中将其设置为只占用一行时,文本被截断。我该如何制作才能自动调整文本的字体大小,使其只占用一行而不被截断?

I am writing an app, in which part of it displays a line of text. there are certain scenarios where that line of text will take up more than one line. the problem is that I only want it to take up one line, and when I set it up either in java or in xml to only take up one line, the text is cut off. how would I make it so that it automatically adjusts the font size of the text so that it will only take up one line without being cut off?

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

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

发布评论

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

评论(2

坏尐絯℡ 2024-11-06 18:40:59

将比例与 Paint.measureText() 一起使用:(

文本大小/measureText宽度)=(perfectSize/screenWidth)

求解完美文本大小:

perfectSize = (text size / measureText width) * screenWidth;

您可以使用getWindowManager().getDefaultDisplay().getWidth()找到屏幕宽度来自 Display 类。

把它变成了一道数学题!

Use proportions along with Paint.measureText():

(text size / measureText width) = (perfectSize / screenWidth)

Solving for the perfect text size:

perfectSize = (text size / measureText width) * screenWidth;

You can find the screen width with getWindowManager().getDefaultDisplay().getWidth() from the Display class.

Turned it into a math problem!

擦肩而过的背影 2024-11-06 18:40:59

如果您使用 绘制#measureText

基本上,您将从小于 TextView 高度的字体高度开始,然后迭代(或进行二分搜索)不同大小的字体,直到找到一种小于您的 TextView 宽度的字体。 文本视图

它需要一些实现,因为框架没有提供“自动”方式。

This isn't too hard to do if you use Paint#measureText.

Basically you would start with a font height smaller than the height of your TextView and then iterate (or do a binary search) through fonts of varying sizes until you find one that measures to smaller than the width of your TextView.

It requires some implementation, as there is no "automatic" way provided by the framework.

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