TextRendering in TextView and EditText

I have a simple view:

<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 and EditText. When I set the same text in both of them, it seems that each of them conveys the text in different ways. as below: Top view is EditText and below is TextView

I use StaticLayout to measure the text and determine the text boundaries in each row, and I have to set the text in the TextView (so the user cannot edit or select it).

But it seems that StaticLayout text border calculations match an EditText, not a TextView.

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

My question is why text rendering is different in TextView and EditText, and how can I measure text using StaticLayout and set text in TextView so that each offset of the beginning and end of each line exactly matches the result that the user sees after setText

+4

Source: https://habr.com/ru/post/1665127/


All Articles