带有 ellipsize 属性的 Android TextView 从原来的位置移动了

标签 android textview attr longtext

我发现 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/

相关文章:

Android Studio Java错误

java - 警报对话框自行取消

android - 如何在 textview 上显示最新的追加文本?

android - 如何淡出 TextView 中最后一行的结尾?

jquery - 删除某个类之后的所有类

android - 使用启动器标志在 Activity 中获得额外的未决 Intent

android 更改 TextView (这段代码可以吗?)

php - 如果缺少图像文件名,如何使用 php 自动添加图像 "alt"属性?

android - 看起来像 AlertDialog 的自定义对话框

旋转显示时Android模拟器损坏