[英]Scale font size based on screen density
I know in Android that you can scale the images based on screen densities. 我知道在Android中,您可以根据屏幕密度缩放图像。 For instance, if mdpi is the baseline, 例如,如果mdpi是基准,
Are there similar values for font size? 字体大小是否有相似的值?
what you need to do is to perform a conversion using the scaleDensity
( here the documentation) value, member of the DisplayMetrics class. 您需要执行的操作是使用DisplayMetrics类成员的scaleDensity
( 在此为文档)值进行转换。 From the documentation 从文档中
A scaling factor for fonts displayed on the display. 显示屏上显示的字体的比例因子。 This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size. 这与密度相同,除了可以在运行时根据用户对字体大小的偏好以较小的增量进行调整。
Exactly the same values might be used for font size 字体大小可以使用完全相同的值
android:textSize="18dp"
Although it's recommended to use sp
(scale independent pixels). 尽管建议使用sp
(与比例无关的像素)。 It's almost the same thing as dp
, but it might scale-up based on user accessibility settings (for instance, if you haw low vision, you can scale-up font size affecting how sp
is calculated). 它与dp
几乎相同,但是它可能会根据用户可访问性设置进行放大(例如,如果您视力不好,则可以放大字体大小,从而影响sp
的计算方式)。 So, in the end it becomes: 因此,最终它变成:
android:textSize="18sp"
Once again, if you're using default settings, 1dp == 1sp 再一次,如果您使用默认设置,则1dp == 1sp
如果要以编程方式执行此操作:
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
res
folder 在res
文件夹中创建这5个文件夹 values-ldpi
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
dimens.xml
in each folder. 并在每个文件夹中添加dimens.xml
。 <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="font_size">72sp</dimen> </resources>
Depending of Screen density change the font size. 根据屏幕密度更改字体大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.