本文介绍了Android TextView setTextSize 错误地增加了文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 TextView 的扩展.getTextSize()setTextSize() 没有被覆盖,我没有扩展这些方法.编程 1.6,API 级别 4.

This is in an extension of TextView. getTextSize() and setTextSize() are not overridden, I do not extend those methods. Programming in 1.6, API level 4.

此代码中的循环每次迭代都会导致 size 乘以 1.5,例如如果 size 最初从 getTextSize 读取 200,则调用 setTextSize(size),再次调用 getTextSize 会读回 300.

The loop in this code causes size to be multiplied by 1.5 every time it iterates, e.g. if size initially reads 200 from getTextSize, then setTextSize(size) is called, getTextSize called again reads back 300.

public void shrinkTest() {
    float size = this.getTextSize();
    while (size > 8) {
        this.setTextSize(size);
        size = this.getTextSize();
    }
}

这是为什么?

推荐答案

嘿,混合单位问题.似乎有点违反直觉,但它很容易解决.默认方法 setTextSize(float) 假定您输入的是 sp 单位(缩放像素),而 getTextSize() 方法返回精确像素尺寸.

Heh, mixed units problem. Seems a little counterintuitive, but it's an easy fix. The default method setTextSize(float) assumes you're inputting sp units (scaled pixels), while the getTextSize() method returns an exact pixel size.

您可以通过使用备用 setTextSize(TypedValue, float) 来解决此问题,如下所示:

You can fix this by using the alternate setTextSize(TypedValue, float), like so:

this.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

这将确保您使用相同的单位.

This will make sure you're working with the same units.

这篇关于Android TextView setTextSize 错误地增加了文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 03:50