简体   繁体   English

如何根据分辨率和dpi android设置正确的字体大小

[英]How to set correct font size based on the resolution and dpi android

In my Layout there are 3 buttons which are horizontally equally divided using weight. 在我的布局中有3个按钮,使用重量水平均分。 Now I am setting the custom font Roboto on the button. 现在我在按钮上设置自定义字体Roboto。

The problem is how to calculate the size on the font based on resolution and dpi, so that it looks proper on all sizes of android devices. 问题是如何根据分辨率和dpi计算字体大小,以便在所有大小的Android设备上看起来都合适。

Option #1: Specify the font size in sp , which takes density and user-requested font scaling into account. 选项#1:在sp指定字体大小,它考虑了密度和用户请求的字体缩放。

Option #2: Specify the font size in dp , which takes density into account, but ignores the user's requested font scaling. 选项#2:在dp指定字体大小,它考虑了密度,但忽略了用户请求的字体缩放。

Option #3: Use the logic from AutoScaleTextView to create an AutoScaleButton that changes its font size to fit the available space (even though this means that each button would have a different font size) 选项#3:使用AutoScaleTextView的逻辑创建一个AutoScaleButton ,它可以改变字体大小以适应可用空间(即使这意味着每个按钮的字体大小也不同)

Option #4: Extend Option #3 by having a collection of AutoScaleButton instances negotiate a common font size among them, picking the smallest one that fits all three buttons, so that the font size is the same (even though the text might not fill two of the three buttons, depending on the caption length in characters, etc.). 选项#4:扩展选项#3,让AutoScaleButton实例的集合在它们之间协商一个共同的字体大小,挑选适合所有三个按钮的最小字体,以便字体大小相同(即使文本可能不填充两个三个按钮,取决于字符的标题长度等)。

take dimension variable for all dpi 获取所有dpi的维度变量

<dimen name="detail_photo_size">80sp</dimen>

in values-hdpi,values-ldpi,values-mdpi and then use like: 在values-hdpi,values-ldpi,values-mdpi然后使用如下:

txtballoon_item_title
.setTextSize(getResources().getDimension(R.dimen.detail_photo_size));

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

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