前言

Android开发中,TextView设置android:singleLine=”true”时Android Studio提示用android:maxLines=”1″替换。但是发现使用maxLines之后,没有跑马灯了。

记录一下maxLines和singleLine区别,方便自己查阅。

正文

下面通过例子看两个的区别。

例子

下面是maxLines和singleLine的测试xml。

MarqueeTextView就是封装TextView,之前介绍过,略。

  1. <com.biumall.demo.MarqueeTextView
  2.   android:layout_width="400dp"
  3.   android:layout_height="wrap_content"
  4.   android:layout_gravity="center"
  5.   android:ellipsize="end"
  6.   android:focusable="true"
  7.   android:maxLines="1"
  8.   android:padding="10dp"
  9.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  10.   android:textSize="30sp" />
  11.    
  12. <com.biumall.demo.MarqueeTextView
  13.   android:layout_width="400dp"
  14.   android:layout_height="wrap_content"
  15.   android:layout_gravity="center"
  16.   android:ellipsize="marquee"
  17.   android:focusable="true"
  18.   android:marqueeRepeatLimit="marquee_forever"
  19.   android:padding="10dp"
  20.   android:singleLine="true"
  21.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  22.   android:textSize="30sp" />
复制

运行起来后会发现

  1. 都是显示一行

  2. singleLine配置的的有跑马灯,maxLines没有。

从效果来看,如果你要跑马灯就必须使用singleLine。

简单记录singleLine和maxLines的区别

区别

通过看源码和看其他网友的博客,总结如下。

maxLines
  1. <!-- Makes the TextView be at most this many lines tall.
  2. # 翻译为 :使TextView的高度最多为这么多行。
  3. -->
  4. <attr name="maxLines" format="integer" min="0" />
复制

也就是说,当我设置如下时

  1. android:maxLines="1"
复制

会使TextView显示一个行的高度内容,超过一行高度的内容就越界不显示,并不会把多行内容放在一行中显示。

因此,为了显示完整[笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^]的内容,我下面maxLines不同行数的显示效果。

  1. <com.biumall.demo.MarqueeTextView
  2.   android:layout_width="400dp"
  3.   android:layout_height="wrap_content"
  4.   android:layout_gravity="center"
  5.   android:layout_marginTop="20dp"
  6.   android:background="@android:color/darker_gray"
  7.   android:padding="10dp"
  8.   android:maxLines="1"
  9.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  10.   android:textSize="30sp" />
  11. <com.biumall.demo.MarqueeTextView
  12.   android:layout_width="400dp"
  13.   android:layout_height="wrap_content"
  14.   android:layout_gravity="center"
  15.   android:layout_marginTop="20dp"
  16.   android:background="@android:color/darker_gray"
  17.   android:padding="10dp"
  18.   android:maxLines="2"
  19.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  20.   android:textSize="30sp" />
  21. <com.biumall.demo.MarqueeTextView
  22.   android:layout_width="400dp"
  23.   android:layout_height="wrap_content"
  24.   android:layout_gravity="center"
  25.   android:layout_marginTop="20dp"
  26.   android:background="@android:color/darker_gray"
  27.   android:padding="10dp"
  28.   android:maxLines="3"
  29.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  30.   android:textSize="30sp" />
  31. <com.biumall.demo.MarqueeTextView
  32.   android:layout_width="400dp"
  33.   android:layout_height="wrap_content"
  34.   android:layout_gravity="center"
  35.   android:layout_marginTop="20dp"
  36.   android:background="@android:color/darker_gray"
  37.   android:padding="10dp"
  38.   android:maxLines="4"
  39.   android:text="笔友城堡[www.biumall.com],记录美好生活。阅读是一种生活方式^_^"
  40.   android:textSize="30sp" />
复制

从下图就可以看明白,当设置maxLines为1行时,其他的是被影藏了,并只显示一行高度的内容,只有行数设置阅读,显示的内容就多。

当然,也可以看出设置maxLines替代singleLine时跑马灯失效的原因了。

简单记录singleLine和maxLines的区别

singleLine
  1. <!-- Constrains the text to a single horizontally scrolling line
  2.     instead of letting it wrap onto multiple lines, and advances
  3.     focus instead of inserting a newline when you press the
  4.     enter key.
  5.    
  6.     # 翻译:将文本限制为一行水平滚动的文本,而不是让它换行到多行...
  7. -->
  8. <attr name="singleLine" format="boolean" />
复制

也就是如果我们设置了

  1. android:singleLine="true"
复制

会使TextView中的内容全部一行显示。加上ellipsize等的配置就会让文本跑起来。

参考文章

  1. singleLine=”true” 和 maxLines=”1″的区别

  2. singleLine 和 maxLines

相关文章

暂无评论

none
暂无评论...