TextView.setTextSize行为异常 - 如何动态地设置的TextView的文本大小不同势屏幕文本、异常、屏幕、大小

2023-09-12 22:08:30 作者:弦断い囿谁听

调用 TextView.setTextSize()的工作不正常。调用 setTextSize 之后,如果我们得到了一个 getTextSize 它返回一个更高的值,我们将其设置为更早。

Calling TextView.setTextSize() is working abnormally. Right after the call to setTextSize if we get a getTextSize its returning a much higher value that what we set it to earlier.

下面就是我们正在做的:

Here's what we're doing:

zoomControl.setOnZoomInClickListener(new OnClickListener() {
    public void onClick(View view) {
        float size = mViewShabad.getTextSize() + 1;
        textView.setTextSize(size);
    }
});

有没有人见过这个?

Has anyone seen this before?

推荐答案

这里的区别是,在 setTextSize(INT尺寸)方法,默认情况下,单位类型是SP或缩放像素。该值将是为每个屏幕像素密度(LDPI,MDPI,华电国际)不同的像素尺寸。

The difference here is that in the setTextSize(int size) method, the unit type by default is "sp" or "scaled pixels". This value will be a different pixel dimension for each screen density (ldpi, mdpi, hdpi).

getTextSize(),在另一方面,返回文本的实际像素尺寸。

getTextSize(), on the other hand, returns the actual pixel dimensions of the text.

您可以使用 setTextSize(INT单元,浮点大小)来指定单位类型。这个常量的值可以在类的TypedValue中找到,但其中一些是:

You can use setTextSize(int unit, float size) to specify a unit type. The constant values for this can be found in the TypedValue class, but some of them are:

TypedValue.COMPLEX_UNIT_PX   //Pixels

TypedValue.COMPLEX_UNIT_SP   //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP  //Device Independent Pixels