我有一个像这样的简单布局:
<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之后看到的结果完全匹配
解决方法
尝试为两个视图设置相同的
breakStrategy和
https://developer.android.com/reference/android/widget/TextView.html#attr_android:hyphenationFrequency
enter link description here,因为它们可能与EditText和TextView默认值不同.