我发现 Android TextView 和 ellipsize 属性有一个奇怪的行为。 当文本很长并且应用了 ellipsize 属性时,文本的原始位置会丢失,文本会显得有点靠上。
是的,我可以通过编程方式解决它,但我想知道它是否可能是一个错误或我做错了什么。
一个非常简单的例子来测试它。 更改两个 TextView 的文本以查看椭圆化的文本或不像我在上一个屏幕截图中显示的那样。如果您比较屏幕截图,就会发现问题所在。
我已经用“dp”和“sp”对其进行了测试,看看它是否可能是不同的行为。但事实并非如此。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:background="@android:color/holo_green_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23.3dp"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Hello World! (dp textSize)"
android:textColor="@android:color/black"
android:textSize="25dp" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="150dp"
android:background="@android:color/holo_blue_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="123.2dp"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Hello World! (sp textSize)"
android:textColor="@android:color/black"
android:textSize="25sp" />
</RelativeLayout>
Short texts under the blocks color perfectly glued
Simply changing the texts with a long string and the texts aren't perfectly glued
解决方案:
解决方案是使用属性“singleLine”,然后问题就消失了。但是 AndroidStudio 告诉我“singleLine is deprecated”。
最佳答案
我对字体“Courier New”有类似的问题 - 当 1 行文本被省略时,它向下移动了几个像素。 我的问题的解决方案是添加:
android:includeFontPadding="false"
到我使用该字体的任何地方。
关于带有 ellipsize 属性的 Android TextView 从原来的位置移动了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39944535/