Android – textrendering in textview and EditText

I have a simple layout like this:

<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 them, it seems that each text presents the text in a different way. As follows:

I am using staticlayout to measure the text and identify the text boundary in each line. I must set the text to textview (so the user cannot edit or select it)

But it seems that the staticlayout text Boundary Calculation matches EditText instead of textview

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

My question is why the text rendering in textview and EditText is different and how I use staticlayout to measure the text and set the text to textview so that the start and end offsets of each line exactly match the results seen by the user after settext

resolvent:

Try setting the same breakstrategy and for both views https://developer.android.com/reference/android/widget/TextView.html#attr_ Android: hyphenationfrequencyenter link description here, because they may be different from the default values of EditText and textview

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>