【问题标题】:Android TextView with ellipsize attribute moved from the original position具有 ellipsize 属性的 Android TextView 从原始位置移动
【发布时间】:2017-02-18 01:32:16
【问题描述】:

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

【问题讨论】:

  • 解决方法是使用属性'singleLine'然后问题就消失了。但 AndroidStudio 告诉我“singleLine 已弃用”。

标签: android textview attr longtext


【解决方案1】:

我在使用“Courier New”字体时遇到了类似的问题——当 1 行文本被椭圆化时,它向下移动了几个像素。 我的问题的解决方案是添加:

android:includeFontPadding="false"

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

【讨论】:

  • 我会看看这个选项。谢谢!
猜你喜欢
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多