简体   繁体   English

文字大小相同,但在其他设备上更大

[英]Same text size but it larger on other devices

I have a TextView in which I programmatically setup the text size in 'SP'. 我有一个TextView在其中以编程方式在“ SP”中设置文本大小。

tv.setTextSize(spToPx(5, context));

public static int spToPx(float sp, Context context) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}

First screenshot shows how it should look (and how it looks on my Xiaomi Redmi and Doogee HT7 第一个屏幕截图显示了它的外观(以及在我的小米Redmi和Doogee HT7上的外观)

第一个屏幕截图显示了它的外观(以及在我的小米Redmi和Doogee HT7上的外观)

Second screenshot shows how it looks on other devices with higher or the same dpi. 第二张屏幕截图显示了在更高或相同dpi的其他设备上的外观。

第二张屏幕截图显示了在更高或相同dpi的其他设备上的外观。

Normally, if I understand correct, text with the same size in 'dp' or 'sp' should looks the same on other devices (with the same screen size) because it automatically converts 'dp' or 'sp' to the needed amount of pixels depending on dpi. 通常,如果我理解正确,则“ dp”或“ sp”中具有相同大小的文本在其他设备(具有相同的屏幕大小)上应该看起来相同,因为它会自动将“ dp”或“ sp”转换为所需的像素取决于dpi。

After reading Android Developer Guideline about devices with different density I was expecting that text would be smaller if I use the same text size on devices with higher dpi. 阅读有关密度不同的设备的Android开发人员指南后,我期望如果在具有更高dpi的设备上使用相同的文本大小,则文本会更小。 But now I have a situation that I really don't understand. 但是现在我确实不了解这种情况。

If you are using values in sp and the user changes the default text size for his device from Settings--->Display---->TextSize, your TextViews will be affected. 如果您在sp中使用值,并且用户从Settings ---> Display ----> TextSize更改其设备的默认文本大小,则TextViews 受到影响。 If on the other hand you use dp values, then no matter what text size the user sets as default, the TextViews in your app will remain the same. 另一方面,如果您使用dp值,则无论用户将默认文本大小设置为什么,应用程序中的TextView都将保持不变。 This is ofcourse discouraged by Google as bad practice, since the user should be able to change text sizes but it would be what you are looking for. 当然,Google不建议您这样做,因为用户应该可以更改文本大小,但这正是您要的内容。

So, use (TypedValue.COMPLEX_UNIT_DIP,number of your choosing) instead of (TypedValue.COMPLEX_UNIT_SP, sp) 因此,请使用(TypedValue.COMPLEX_UNIT_DIP,number of your choosing)而不是(TypedValue.COMPLEX_UNIT_SP, sp)

Try this, Add this property in your xml. 试试这个,在您的xml中添加此属性。 It will change textsize based on all phone. 它将根据所有电话更改文本大小。

style="@android:style/TextAppearance.DeviceDefault.Medium" /*for normal text */
style="@android:style/TextAppearance.DeviceDefault.Small" /*for small text */
style="@android:style/TextAppearance.DeviceDefault.Large" /*for large text */

Hope your problem will solve. 希望你的问题能解决。

You can add dimens file to your resources . 您可以将dimens文件添加到资源中。 thats how I do it 那就是我的做法 在此处输入图片说明

heres the link where I got the answer 这是我得到答案的链接

The primary difference between an sp and a dp is that sp's preserve a user's font settings. sp和dp之间的主要区别是sp保留了用户的字体设置。 Users who have larger text settings for accessibility will see font sizes match their text size preferences. 具有辅助功能的较大文本设置的用户将看到字体大小与其文本大小首选项相匹配。

so you could check the settings of two devices and see if there is a difference between them. 因此您可以检查两个设备的设置,看看它们之间是否存在差异。 在此处输入图片说明

I still didn't find right explanation to this issue. 我仍然没有找到对此问题的正确解释。 The only theory I have is next: 我唯一的理论是下一个:

There are density buckets (120 dpi, 160 dpi, 240 dpi, 320 dpi, 480 dpi, 640 dpi). 有密度桶(120 dpi,160 dpi,240 dpi,320 dpi,480 dpi,640 dpi)。 Screen density of your device will be determinated as the closest bucket. 设备的屏幕密度将确定为最接近的值区。 For example, if your device has 401 dpi it will be counted as 480 dpi. 例如,如果您的设备具有401 dpi,它将被计为480 dpi。 So, when I create and add view with size of 20 dp it will convert into more pixels than needed. 因此,当我创建并添加大小为20 dp的视图时,它将转换为比所需pixels更多的pixels It the hardest for devices that have density right between two buckets. 对于密度恰好在两个存储桶之间的设备,这是最难的。 I also checked density of all devices that had wrong image display (like on image 2 ) and it was almost right between two buckets... 我还检查了所有显示错误图像的设备的密度(如image 2 ),并且几乎在两个存储桶之间。

I still don't know why this happens only when I create and add view dynamycally, but the only way to solve this problem was the creating of separate layout with all presetted views instead of adding these views dynamically. 我仍然不知道为什么只有在动态创建和添加视图时才会发生这种情况,但是解决此问题的唯一方法是使用所有预设视图创建单独的布局,而不是动态添加这些视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM