本文介绍了TEXTSIZE填补屏幕宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序倒计时功能。
每一秒的定时器设置当前时间,一个TextView。
所以字符串是:56:05,56:04,56:03,56:02......

I have countdown functionality in my app.Every second a timer sets the current time to a TextView.So strings are: "56:05","56:04","56:03","56:02"...

我想使文本尺寸尽可能大

I want to make the text size as big as possible

所以我写了下面的code。

Therefore I've written the following code.

private void measureAndSetText(String text) {

    Paint pMeasure = new Paint();
    Integer iWidth = _tvContent.getWidth();

    Float maxTextSize = 1000f;

    pMeasure.setTextSize(maxTextSize);
    pMeasure.setFakeBoldText(true);

    Float fCurrentWidth = pMeasure.measureText(text);

    while (fCurrentWidth > iWidth) {
        pMeasure.setTextSize(maxTextSize -= 1);
        fCurrentWidth = pMeasure.measureText(text);
    }

    _tvContent.setText(text);
    _tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
}

这code似乎在我的注2,LG擎天柱4X HD,Galay ACE和其他一些工作。
却没有关于我的Xperia中的Z横向模式。
我想原因是全高清显示屏,但我不明白为什么。
上了Xperiaž只是:符号显示在横向模式。因此,我认为该文本被包裹,但我不知道为什么。
这将是有意义的我,如果我设置它的文字大小比屏幕高度较高(在横向模式下这实际上是屏幕的宽度 - > 1080),但事实并非如此。
当我尝试设置一个较长的文本 - >asdfasd ASDF这是正确显示

This code seems to work on my Note2, Lg Optimus 4x HD, Galay ACE and some others.But not on my Xperia Z in landscape mode.I guess the reason is the Full Hd Display but I don't understand why.On the Xperia Z just the ":" sign is displayed in landscape mode. So I think the text is wrapped but I don't know why.It would make sense to me if the text size I set to it is higher than the screen height (in landscape mode this is actually screen width -> 1080) but this isn't the case.When I try to set a longer text -> "asdfasd asdf" it is correctly displayed.

有人能指出我的问题?

干杯,
斯特凡

Cheers,Stefan

更新:
我想通了,我持有我的TextView的宽度iWidth变量(_tvContent.getWidth())的值为1770。
怎么会是这样,因为我的Xperia Z具有1920×1080只???

UPDATE:I figured out that my iWidth variable which holds the width of my TextView (_tvContent.getWidth()) has the value 1770.How can that be since my Xperia Z has just 1920x1080???

所以,我认为这可能是TextView.getWidth失败(),并增加了以下code:

So I thought that it may be a failure in TextView.getWidth() and added the following code:

    Display display3 = getWindowManager().getDefaultDisplay();
    Point size3 = new Point();
    display3.getSize(size3);
    int width = size3.x;

    Display display1 = getWindowManager().getDefaultDisplay(); 
    int width1 = display1.getWidth();  // deprecated
    int height1 = display1.getHeight();  // deprecated


    Display display = getWindowManager().getDefaultDisplay(); 
    Integer iWidth = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated

结果:

点(1080,1776)

Point(1080, 1776)

显示ID 0:DisplayInfo {Integrierter Bildschirm,应用1080点¯x1776年实际1080点¯x1920年,最大的应用1794点¯x1701年最小的应​​用1080点¯x1005 60.0 FPS,旋转0,密度480,442.451点¯x443.345 DPI,层堆叠0,类型BUILT_IN,地址为null,FLAG_SECURE,FLAG_SUPPORTS_PROTECTED_BUFFERS},{DisplayMetrics密度= 3.0,宽= 1080,高度= 1776,scaledDensity = 3.0,xdpi = 442.451,ydpi = 443.345}的isValid = TRUE

Display id 0: DisplayInfo{"Integrierter Bildschirm", app 1080 x 1776, real 1080 x 1920, largest app 1794 x 1701, smallest app 1080 x 1005, 60.0 fps, rotation 0, density 480, 442.451 x 443.345 dpi, layerStack 0, type BUILT_IN, address null, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}, DisplayMetrics{density=3.0, width=1080, height=1776, scaledDensity=3.0, xdpi=442.451, ydpi=443.345}, isValid=true

这是索尼的网站?

推荐答案

我实现我自己的TextView有自己的OnDraw后,我见过的,有在LogCat中的错误:ERROR / OpenGLRenderer(2503):字体大小大适合在高速缓存中。

After I implemented my own TextView with its own OnDraw I've seen that there is an error in LogCat: ERROR/OpenGLRenderer(2503): Font size to large to fit in cache.

经过短暂的研究,我figgured指出,这是真正在Android中的一个Bug。

After a short research I figgured out that this is really a Bug in Android.

Addding此行我的自定义的TextView的伎俩对我来说:

Addding this line to my custom TextView did the trick for me:

this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

这篇关于TEXTSIZE填补屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:40