android中如何自动调整textSize?
我正在编写一个应用程序,其中部分显示一行文本。在某些情况下,该行文本将占用多行。问题是我只希望它占用一行,当我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将比例与 Paint.measureText() 一起使用:(
文本大小/measureText宽度)=(perfectSize/screenWidth)
求解完美文本大小:
您可以使用
getWindowManager().getDefaultDisplay().getWidth()
找到屏幕宽度来自 Display 类。把它变成了一道数学题!
Use proportions along with Paint.measureText():
(text size / measureText width) = (perfectSize / screenWidth)
Solving for the perfect text size:
You can find the screen width with
getWindowManager().getDefaultDisplay().getWidth()
from the Display class.Turned it into a math problem!
如果您使用 绘制#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 yourTextView
.It requires some implementation, as there is no "automatic" way provided by the framework.