【问题标题】:Unexpected font size change in Android at the level of decimalsAndroid 中的小数级别意外的字体大小变化
【发布时间】:2017-12-21 23:06:34
【问题描述】:

我注意到当在android textview中使用setTextSize方法指定字体大小时,字体大小的小数是根据分辨率确定的,我通过使用getTextSize方法实现了这一点。

我正在处理一个字体敏感度非常高的项目。在这个项目中,组件的位置和大小必​​须绝对零变化。

例如,当我指定字体大小(例如 30.777777777)时,我不希望 Android 对其进行任何更改。

实际样本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.magazinetest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAMPLE TEXT"
        android:textSize="40dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAMPLE TEXT"
        android:textSize="40.2dp" />

</LinearLayout>

完全相同的结果:40 dp40.2 dp

之间没有变化

https://i.stack.imgur.com/A4s2c.png

有什么办法可以防止这种情况发生吗?

注意:IOS 不会对字体大小进行这样的更改。

【问题讨论】:

  • 我不明白你所说的 android 自己改变字体大小的意思。你能给我一段可重现的代码来证明这一点吗?
  • 我添加了示例代码和图片。

标签: android fonts size decimal


【解决方案1】:

尝试在dp 中设置文本大小。如果您在 sp 中设置 textsize,则可能会发生更改。阅读文档Link

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2015-12-09
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多