如何使 TextView 中的文本大小在不同的屏幕尺寸和密度下保持一致?

How do I keep Text Size in TextView consistent over different screen sizes and densities?

我已经阅读了很多关于此的问答,其中 none 有效。他们很可能已经过时了。当然有一个简单的解决方案。我正在开发一个 UI,它是一个静态图片背景(尽管没有密度桶,但它在所有设备上都能很好地缩放),背景上有 TextViews 布局(见图片)。

  1. 看起来不错:

  1. 看起来不太好:

我尝试过以多种不同的方式布置文本,目前是从中心开始(有一个不可见的图像称为中心点,因为我不确定如何布置距中心的像素距离)。我试过 spdp 它们的反应都一样。我只希望我的文字始终与图像对齐。如果可以解决问题,我可以改用按钮​​,但我也不确定该怎么做。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".Main2Activity">

    <TextView
        android:id="@+id/mpView"
        android:layout_width="wrap_content"
        android:layout_height="0sp"
        android:layout_marginTop="208dp"
        android:layout_marginEnd="52dp"
        android:autoSizeTextType="none"
        android:onClick="mpRanges"
        android:text="@string/mp"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/centreDot"
        app:layout_constraintTop_toTopOf="@+id/centreDot" />

您可以使用this library,这将在不同的屏幕尺寸上保持相同的字体大小。

如果您的图片没有固定dp 的特定尺寸,您将无法设置具有固定dp 值的TextView 位置。

假设您的图片宽度为 100 像素,并缩放到设备宽度。

在一台设备上显示宽度为 200dp,因此 100px 的图像缩放为 200dp。 在另一台设备上,显示宽度为 400dp,因此 100px 缩放为 400dp。 现在让我们以 dp 为单位计算图像中心:第一种情况是 100dp,第二种情况是 200dp!这就是为什么你不能让它在所有设备上看起来都很好。

要解决此问题,您需要创建自定义 ViewGroup(可能是 FrameLayout 的子项)。在运行时,您将能够获得真实的图像大小并计算所有 TextView 相对于您的图像的位置。似乎您甚至不需要实现 onMeasure() 和 onDraw(),只需实现 onLayout()。

创建自定义视图的官方文档:https://developer.android.com/guide/topics/ui/custom-components。但也许你会通过谷歌搜索 "android create custom viewgroup" 或类似的东西找到更好解释的文章。

-

我仍然更喜欢我的第一个答案,但会添加另一个答案:也许您将能够通过 ConstraintLayout 中的百分比定位来完成您需要的操作。您可以使用 layout_constraintGuide_percent 为图像上的每个点(垂直和水平)创建指南,并将 TextView 粘贴到它上面。 0.2 和 0.5 - 用图像中点中心的百分比位置替换此值。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/gd1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.2" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/gd2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@id/gd1"
    app:layout_constraintEnd_toEndOf="@id/gd2"
    app:layout_constraintStart_toStartOf="@id/gd2"
    app:layout_constraintTop_toTopOf="@id/gd1" />