美文网首页
android textview限制显示几行+省略号

android textview限制显示几行+省略号

作者: hao_developer | 来源:发表于2021-01-06 17:15 被阅读0次

    效果图:


    image.png

    1:布局

     <TextView
        android:id="@id/tvTest"
        android:paddingLeft="@dimen/p10"
        android:paddingRight="@dimen/p10"
        android:text="上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上"
        android:layout_width="match_parent"
        android:maxLines="2"
        android:ellipsize="end"
        android:layout_height="wrap_content"/>
    

    2:布局中添加以下配置无效,需要添加第三步的代码
    \color{red}{android:maxLines="2"}\
    \color{red}{android:ellipsize="end"}\


    3:需要在activity中设置以下代码:

    if (tvTest.lineCount > 2){
         val lineEndIndex = tvTest.layout.getLineEnd(1) //拿取第二行最后一个字符的位置
         val text = "" + (tvTest.text.subSequence(0,lineEndIndex)) + "..." //进行截取+拼接
         tvTest.text =text
    }
    

    实现以上三步就可以实现以上的效果图

    相关文章

      网友评论

          本文标题:android textview限制显示几行+省略号

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