我发现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已弃用”。

最佳答案

我对“ Courier New”字体也遇到了类似的问题-当1行文本变为椭圆形时,它向下移动了几个像素。
解决我的问题的方法是添加:

android:includeFontPadding="false"


到我使用该字体的任何地方。

07-24 09:47
查看更多