簡體   English   中英

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

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

我已經閱讀了很多關於此的問答,但沒有一個有效。 它們很可能已經過時了。 當然,對此有一個簡單的解決方案。 我正在開發一個 UI,它是一個靜態圖片背景(盡管缺乏密度桶,但它在所有設備上都可以很好地縮放),在背景上有 TextViews 布局(見圖片)。

  1. 看起來不錯:

看起來不錯

  1. 不好看:

    看起來不太好

我已經嘗試以多種不同的方式布置文本,目前從中心開始(有一個稱為 centerdot 的不可見圖像,因為我不確定如何布置距中心的像素距離)。 我試過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" />

您可以使用這個庫,這將在不同的屏幕尺寸上保持相同的字體大小。

如果您的圖像不是由 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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM