【问题标题】:TextView: Get it to truncate without respect to spaces between wordsTextView:让它截断而不考虑单词之间的空格
【发布时间】:2018-04-05 13:44:53
【问题描述】:

如何配置 TextView 在单词中间截断?

所以如果我有 text="Some Text" 我希望它显示为“Some Te”,假设宽度支持它。

我看到的是“一些”;即使有足够的空间容纳更多字符,它也会截断整个单词“Text”。

【问题讨论】:

  • 你介意结尾有省略号吗?如Some T...
  • 没有省略号的余地,所以我介意。我只是希望它截断。空间紧。可能不是最好的设计,但那是另一回事。
  • 您希望它在截断标记处剪切末尾的字符,还是只希望它显示尽可能多的字母?例如,如果“Some Te”可以显示,但“e”只能部分显示,您是希望它显示“Some T”还是“Some Te”而“e”部分显示

标签: android textview truncate


【解决方案1】:

这对我有用:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    android:ellipsize="marquee"/>


或以“...”结尾:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    />


虽然使用android:lines 不起作用:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:lines="1"
    android:ellipsize="marquee"/>

这很可能是一个错误,我相信我已经看到了一些关于此的错误报告。

【讨论】:

  • 好的,谢谢。虽然我也在尝试与上面的第一个 xml 相同,但是使用 android:scrollHorizo​​ntally="true" (下面的信用 Otra 将我放在那个轨道上),以避免弃用的 singleLine 属性。似乎也有效。
【解决方案2】:

这是我的 TextView 代码,它“截断”了没有省略号的单词。它并没有完全切断它,它只是让它从边缘跑出来,给人一种被截断的轻微印象。

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:scrollHorizontally="false"
    android:ellipsize="none"
    android:text="@string/hello"
/>

【讨论】:

  • 没有singleLine可以吗?
【解决方案3】:
Found Two ways to Truncating texview.
Text to truncate : "Hello World! sample_text_truncate"
Case 1:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"`enter code here`
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:singleLine="true"/>

Result: Hello World! sample_text_trunc...

Case 2:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:maxLines="1"/>

Result: Hello World!...

So i found difference in singleline and maxlines properties.Unfortunately singleline in depricated.I wanted singleline result.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多