如何动态地设置的TextView的文本大小不同的屏幕 [英] How to set text size of textview dynamically for different screens

查看:101
本文介绍了如何动态地设置的TextView的文本大小不同的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个TextView,并动态地添加到布局。我使用 textView.setTextSize(18)方法来设置size.I测试了三星平板电脑,发现字体大小是太小,屏幕上的文本,然后我改该TEXTSIZE 25,但它是太大仿真器(480 * 800)。我的问题是动态设置文字大小,使其适合于所有的屏幕上。

I am creating a textview and adding to the layout dynamically. I am using textView.setTextSize(18) method to set the text size.I tested it on samsung tablet and found that the font size is too small for this screen then I changed the textsize to 25 but it is too big for an emulator(480*800). My problem is to set text size dynamically so that it fits for all the screens.

推荐答案

大概没有需要使用 LDPI MDPI 华电国际在这种情况下预选赛。

There is probably no need to use the ldpi , mdpi or hdpi qualifiers in this case.

当您在资源文件中定义一个维度,你包括测量单位。如果你使用的SP单元,他们是根据屏幕像素密度使文本在15sp应该出现大致相同的大小不同密度的屏幕比例。照片(设备的实际屏幕像素密度是不会完全匹配成为Android可以推广屏幕像素密度为 120,160,240,320,480和640 DPI 组。)

When you define a dimension in a resource file you include the measurement unit. If you use sp units they are scaled according to the screen density so text at 15sp should appear roughly the same size on screens of differing density.
(The real screen density of the device isn't going to exactly match as Android generalises screen density into 120, 160, 240, 320, 480 and 640 dpi groups.)

在调用 getResources()。getDimension(R.dimen.textsize)将返回的大小以像素为单位。如果使用的SP将通过屏幕像素密度缩放,

When calling getResources().getDimension(R.dimen.textsize) it will return the size in pixels. If using sp it will scaled by the screen density,

调用的setText(浮点)设置在 SP单元的尺寸。这是那里的问题是,
您有一方面像素测量和 SP单元上的其他解决做到这一点:

Calling setText(float) sets the size in sp units. This is where the issue is,
i.e you have pixels measurements on one hand and sp unit on the other to fix do this:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
    getResources().getDimension(R.dimen.textsize));

请注意,你也可以用

getResources().getDimensionPixelSize(R.dimen.textSize);

而不是的getDimension(),它会圆转换为无分数值。

instead of getDimension() and it will round and convert to an non fractional value.

这篇关于如何动态地设置的TextView的文本大小不同的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆