美文网首页
maxLine 遇到的坑

maxLine 遇到的坑

作者: FlatMap2021 | 来源:发表于2018-01-21 19:47 被阅读0次

Android 的TextView 里面有两个属性 singLine 和maxLines 。 从字面意思来理解,这两个都是限制Text的行数。那么singleLine="true" 和maxLine="1" 都是限制为一行,有什么区别呢?
先看看Google Document 的解释:
android:maxLines Makes the TextView be at most this many lines tall.
android:singleLine Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.
可以看出,maxLines 是在限制高度, singleLine 是强制不让换行。具体效果有什么区别呢? 从高度来讲是一样的,两者肯定都显示一行,但从换行的位置来讲就有区别了,maxLines并不会改变其换行的位置,而singleLine则会。从这个角度讲,singleLine的显示会好一些,因为如果超过一行singleLine会在一行内显示,后面加上"...",而maxlines="1" 则不会,它依然会在原来换行的位置换行,所以有时候一行不满,但是却不显示剩下的部分。
我遇到的坑是这样的,webView 的标题过长,需要设置为跑马灯的效果,在使用时我是这样写的代码:


image.png

使用了maxline ="1" 和ellipsize,结果就导致Android部分机型出现crash,友盟统计到的的是5.1手机,具体其他机型crash情况不太确定,后来查一下知道,ellipsize 要与singleLine配合使用才行,如果是其他情况,建议还是使用maxLine, 但是与跑马灯效果配合使用的时候一定要用singleLine。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView  
    android:layout_width="100dip"  
    android:layout_height="wrap_content"  
    android:layout_gravity="center"  
    android:text="走马灯效果的演示"   
    android:singleLine="true"  
    android:ellipsize="marquee"/>  

</LinearLayout>

相关文章

  • maxLine 遇到的坑

    Android 的TextView 里面有两个属性 singLine 和maxLines 。 从字面意思来理解,这...

  • 2018-10-11【Android代码重构使用技巧】

    1.代码重构 android:singleLine=”true”过时 解决方法:使用android:maxLine...

  • Flutter中的RichText控件,没有换行

    问题:Flutter 中RichText 无法换行? 首先想到的是:softWare设置为true,maxLine...

  • TextView多行文本Ellipsize的终极解决方案

    Android的TextView提供了ellipse的功能,就是在文本超过指定行数maxLine后增加...,然而...

  • 遇到的坑

    1.文字两端居中 2.多个异步请求的执行顺序 点击页面上一个按钮发送两个ajax请求时,这两个异步请求会同时发送,...

  • 遇到的坑

    1、 2、每次改完pom.xml后项目的 Language level都会变成7,使用了jdk8新功能的地方都会报...

  • 遇到的坑++

    1.加在一个view的时候报了一个异常 android.view.InflateException: Binary...

  • 遇到的坑

    键盘通知导致UI异常现象描述:本地详情页的inputView输入框,做了很多的功能,由多个view组成,这时点击微...

  • 遇到的坑

    第一次访问是 this page could not be found,刷新一下就好了 路径错误 多了一个 ‘/’

  • iOS开发中遇到过的坑

    iOS开发中遇到过的坑 iOS开发中遇到过的坑

网友评论

      本文标题:maxLine 遇到的坑

      本文链接:https://www.haomeiwen.com/subject/teyqaxtx.html