【问题标题】:TextView's ellipsize not working on maxLines = 1TextView 的椭圆大小不适用于 maxLines = 1
【发布时间】:2014-08-12 15:41:08
【问题描述】:

我真的不知道为什么,但我只能让 ellipsize 在maxLines=2 等上工作。我显示了几句话的描述,然后是一个没有空格的长字符串。

这是 TextView 的样子:

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#757575"
                android:text="@string/gcm_not_registered"
                android:maxLines="1"
                android:ellipsize="end"
                android:id="@+id/login_gcmRegistrationTextView"/>

然后我以编程方式为其设置一个文本,但根据 maxLines 限制,我得到两个不同的结果:

唯一改变的是maxLines,为什么第一张图片中的线没有填好?

【问题讨论】:

标签: android textview


【解决方案1】:

有两种修复方法:

  1. 尝试将android:ellipsize="end"属性更改为android:ellipsize="marquee"
  2. 尝试删除android:maxLines="1" android:ellipsize="end"属性并添加android:singleLine="true"属性。

【讨论】:

  • 2) 工作。但我想知道为什么,当我没有说我希望文本被省略时,它不应该是。我还读到它已被弃用,但查看所有文档我找不到它的一个字。所以谢谢。
【解决方案2】:

此代码适用于我:

在xml中添加:

  • 属性ellipsize:marquee
  • 属性lines:1

在java中:

<yourTextView>.setHorizontallyScrolling(true);
<yourTextView>.setSelected(true);

如果有另一个项目请求“焦点”,您将失去选取框效果。 textView 需要选择状态来防止这种情况。

【讨论】:

    【解决方案3】:

    实际上问题在于可跨文本,如果您设置可跨文本,这将不起作用。除了下面的代码对我有用

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#b3277b"
        android:background="#f7ecff"
        android:layout_below="@id/tv"
        android:text="This is first line\nThis is second line\nThis is third line"
        android:ellipsize="end"
        android:maxLines="2"
        />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2013-09-09
      • 2016-02-22
      • 2013-02-19
      • 2020-12-20
      相关资源
      最近更新 更多