【问题标题】:TextRendering in TextView and EditTextTextView 和 EditText 中的文本渲染
【发布时间】:2017-05-12 20:39:28
【问题描述】:

我有一个这样的简单布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/txtContent2"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:background="#fed9f4"
        android:textSize="22sp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <TextView
        android:id="@+id/txtBelow"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:background="#fed9f4"
        android:textSize="22sp"/>
</LinearLayout>

一个 TextView 和一个 EditText。 当我在它们中设置相同的文本时,似乎每个都以不同的方式呈现文本。如下所示:

我正在使用 StaticLayout 来测量文本并识别每行中的文本边界,并且我必须将文本设置为 TextView(因此用户无法编辑或选择它)。

但似乎 StaticLayout 文本边界计算与 EditText 而不是 TextView 匹配。

StaticLayout layout = new StaticLayout(content, txtContent.getPaint(),
            txtContent.getWidth(),
            Layout.Alignment.ALIGN_NORMAL, 1, lineSpace, false);

我的问题是为什么 TextView 和 EditText 中的文本呈现不同,以及如何使用 StaticLayout 测量文本并将文本设置为 TextView,以便每行的开始和结束偏移量与用户在 setText 后看到的结果完全匹配

【问题讨论】:

    标签: android textview staticlayout


    【解决方案1】:

    尝试为两个视图设置相同的breakStrategyhttps://developer.android.com/reference/android/widget/TextView.html#attr_android:hyphenationFrequencyenter link description here,因为它们的 EditText 和 TextView 默认值可能不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多