• 解决android TextView多行文本(超过3行)使用ellipsize属性无效问题


    布局文件中的TextView属性

    <TextView
    android:id="@+id/businesscardsingle_content_abstract"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:lineSpacingMultiplier="1.0"
    android:lines="6"
    android:text="@string/agrinbusiness_content"
    android:textColor="#7f7f7f"
    android:textSize="13sp" />

     在JAVA代码中控制文本的显示行数

    ViewTreeObserver observer = textAbstract.getViewTreeObserver(); //textAbstract为TextView控件
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
    ViewTreeObserver obs = textAbstract.getViewTreeObserver();
    obs.removeGlobalOnLayoutListener(this);
    if(textAbstract.getLineCount() > 6) //判断行数大于多少时改变
      {
        int lineEndIndex = textAbstract.getLayout().getLineEnd(5); //设置第六行打省略号
        String text = textAbstract.getText().subSequence(0, lineEndIndex-3) +"...";
        textAbstract.setText(text);
      }
      }
    });

  • 相关阅读:
    online_judge_1489
    MybatisPlus 快速开始
    Markdown 基本语法
    python2与python3的区别(1)
    这是我的第一篇博客
    vue 渲染函数&jsx
    计算机进行小数运算出错的原因0.2 + 0.1 = 0.30000000000000004
    最流行的高级语言
    v-cli脚手架
    vue mixins组件复用的几种方式
  • 原文地址:https://www.cnblogs.com/wubingshenyin/p/3310118.html
Copyright © 2020-2023  润新知