如何动态地设置的TextView的文本大小不同的屏幕文本、屏幕、大小、不同

2023-09-12 22:33:55 作者:降妞十巴掌

我创建一个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.

 
精彩推荐
图片推荐