Textview的maxLines大于1行时,"android:ellipsize="end" 省略号失效,这个问题一直都存在,我查找了很多博客,结合自己的实战经验做出分享,希望帮助更多的同仁们。详细如下:

第一步:在布局文件中加上这两个属性 android:ellipsize="end" 和 android:maxLines="2",假设最多不超过2行.。

Textview的maxLines大于1行时,

第二步:写一个工具类。

package com.kqt.live.commom;import android.annotation.SuppressLint;import android.os.Build;import android.view.ViewTreeObserver;import android.widget.TextView;/** * Created with Android Studio. * User: huanggh * Date: 2020/4/23 * Time: 15:20 * Desc: maxLine超过1行时,省略号失效问题解决方案 */public class TextViewUtil {    /**     * 参数:maxLines 要限制的最大行数     * 参数:content  指TextView中要显示的内容     */    public static void setMaxEcplise(final TextView mTextView, final int maxLines, final String content) {        ViewTreeObserver observer = mTextView.getViewTreeObserver();        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                mTextView.setText(content);                if (mTextView.getLineCount() > maxLines) {                    int lineEndIndex = mTextView.getLayout().getLineEnd(maxLines - 1);                    //下面这句代码中:我在项目中用数字3发现效果不好,改成1了                    String text = content.subSequence(0, lineEndIndex - 3) + "...";                    mTextView.setText(text);                } else {                    removeGlobalOnLayoutListener(mTextView.getViewTreeObserver(), this);                }            }        });    }    @SuppressWarnings("deprecation")    @SuppressLint("NewApi")    private static void removeGlobalOnLayoutListener(ViewTreeObserver obs, ViewTreeObserver.OnGlobalLayoutListener listener) {        if (obs == null)            return;        if (Build.VERSION.SDK_INT < 16) {            obs.removeGlobalOnLayoutListener(listener);        } else {            obs.removeOnGlobalLayoutListener(listener);        }    }}

 

第三步:在代码中使用。

大功告成,效果如下:

更多相关文章

  1. CardView 添加背景图片、改变颜色、水波纹效果的方法
  2. Android设置TextView的渐变颜色、方向和动画效果
  3. Android动态效果Animation
  4. Android动画效果-更新中
  5. Android应用开发之RelativeLayout (相对布局)+梅花效果案例
  6. Android控件抖动效果
  7. Android中activity切换动画的两种实现(附种17种动画效果)
  8. 【Android UI设计与开发】第15期:顶部标题栏(六)实现悬浮式顶部和底

随机推荐

  1. 如何开发/修改屏幕锁
  2. Android布局整理
  3. 使用Android(安卓)Studio进行单元测试
  4. 2010.06.05日志:Android(安卓)Gallery左
  5. Android(安卓)更换字体和改变字重
  6. android Activity 之间传递复杂对象
  7. cocos2d-x移植android的编译列出cpp文件
  8. Decompiled .class file,bytecode versio
  9. 处女apk纠结过的技术点
  10. Android|基于Opecv+MTCNN+FaceNet的人脸