TextView文本为Android v4.1 +多行

时间:2014-08-13 13:16:06

标签: android textview multiline word-wrap textwrapping

我在Android中有一个带有Text的TextView,它的长度可以变化很大。它可以是一个单词,也可以是十多个完整的句子。

我的TextView有android:layout_width="match_parent"android:layout_height="wrap_content"。我环顾四周,找到了很多方法让它包裹多行(有些需要多行):

  • android:scrollHorizontally="false"
  • android:singleLine="false"
  • android:ellipsize="none"
  • android:maxLines="10"

所以我的问题是:我应该使用哪个将我的TextView-Text包装在Android 4.1版本中?

PS:我还没有测试过这些,但是因为我在关于文本环绕的SO问题上找到了很多不同的答案,我想知道“最好”的方法是什么(对于我的Android版本)

2 个答案:

答案 0 :(得分:0)

    try setting this attributes:
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="none"
        android:singleLine="false"

here
to elipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.

Say original value pf text view is aaabbbccc and its fitting inside the view

start's output will be : ...bccc

end's output will be : aaab...

middle's output will be : aa...cc

marquee's out put will be : aaabbbccc auto sliding from right to left

答案 1 :(得分:0)

我最终使用了:

<TextView
    ...
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    android:scrollHorizontally="false"
    android:ellipsize="end"
    android:maxLines="10" />

当我查看Eclipse中的singleLine时,它提供了以下java-doc:

  

将文本约束为单个水平滚动线而不是   让它包裹在多行上,并提升焦点而不是   按Enter键时插入换行符。 *不推荐:这个   属性已弃用。使用&#34; maxLines&#34;而是改变布局   静态文本,并使用&#34; textMultiLine&#34; inputType中的标志   属性代替可编辑的文本视图(如果singleLine和   提供inputType,inputType标志将覆盖该值   singleLine)。 [布尔]

所以我现在使用maxLines="10",因为我不希望整个生活故事在TextView中,10行应该是最好的。我已将ellipsize="end"添加到文本末尾有三个点(...),当它有超过10行时。并且scrollHorizontally="false"可以在没有水平滚动条的情况下允许多行。