Android:调整 EditText 字体大小以使文本适合一行
我正在制作一个具有两个 EditText
视图的应用程序,用于输入的 txtOne
和用于输出的 txtTwo
。 txtOne
是可编辑的,而 txtTwo
则不可编辑。
我正在寻找一种简单的方法来在这两个视图中缩放文本的字体大小,以便它始终适合视图而不换行。
我发现了一些使用扩展 TextView
的自定义视图来实现此功能,但它们不适合 EditText
视图,或使用 android.graphics.Paint< /code>,这不是我要找的。
有没有什么方法可以检查 EditText
视图中的文本何时换行?如果是这样,那么做类似的事情就很容易了:
if(txtOne.isWrappingText()) {
txtOne.setTextSize(txtOne.getTextSize() - 2);
}
有谁知道有什么方法来检测这个或替代解决方案吗?
I am making an app that has two EditText
views, txtOne
which is used for input, and txtTwo
which is used for output. txtOne
is editable, and txtTwo
is not.
I am looking for a simple way to scale the font size of the text
in these two views so that it always fits within the view without wrapping to a new line.
I have found a few implementations of this using custom views extending TextView
, but they aren't fit for an EditText
view, or use android.graphics.Paint
, which is not what I am looking for.
Is there any way to check to see when text in an EditText
view is wrapping? If so, then it would be easy enough to do something like:
if(txtOne.isWrappingText()) {
txtOne.setTextSize(txtOne.getTextSize() - 2);
}
Does anyone know of any way to detect this or an alternative solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我删除了我的帖子
基本上,建议的代码包含
EditText
的TextWatcher
,这有助于回答问题。但代码本身就是错误的(我同时测试了它)。我建议阅读这个问题和答案,因为它们解决了非常同样的问题...
I deleted my post
Basically the suggested code contained a
TextWatcher
for theEditText
, this helped to answer the question. But the code itself was just wrong (I tested it meanwhile).I suggest to read this question and answers because they adress the very same issue...