加载中…
个人资料
craining
craining
  • 博客等级:
  • 博客积分:0
  • 博客访问:3,002
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
正文 字体大小:

[转载]textview在多行情况下如何显示省略号

(2012-12-06 17:25:22)
标签:

转载

非常感谢!
据我了解3.0以下版本,设置末尾省略号,最多只支持2行
因此重写TextView很好,
我另做了简单优化:


对setMaxLines()方法做如下修改:

public void setMaxLines(int maxLines) {


        // 预览行数为0-2行时不做处理,当设置为3行时,开始处理末尾要显示的省略号
        switch (maxLines) {
        case 1:
             bEllipsize = false;
            super.setSingleLine(true);
            super.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
            break;
        case 2:
            bEllipsize = false;
            super.setSingleLine(false);
            super.setMaxLines(2);
            super.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
            break;
        case 3:
            bEllipsize = true;
            this.maxLines = maxLines;
            super.setSingleLine(false);
            super.setMaxLines(3);
            super.setEllipsize(null);// 不设置末尾省略号,用代码实现
            break;


        default:
            break;
        }
    }


这样1、2行的时候交给原有SDK实现,超过两行,再手动添加
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.widget.TextView;

public class EllipsizeText extends TextView {

private static final String ELLIPSIS = "...";

public interface EllipsizeListener {
void ellipsizeStateChanged(boolean ellipsized);
}

private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();

private boolean isEllipsized;
private boolean isStale;
private boolean programmaticChange;
private String fullText;
private int maxLines = 2;
private float lineSpacingMultiplier = 1.0f;
private float lineAdditionalVerticalPadding = 0.0f;

public EllipsizeText(Context context) {
super(context);
}

public EllipsizeText(Context context, AttributeSet attrs) {
super(context, attrs);
}

public EllipsizeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void addEllipsizeListener(EllipsizeListener listener) {
if (listener == null) {
throw new NullPointerException();
}
ellipsizeListeners.add(listener);
}

public void removeEllipsizeListener(EllipsizeListener listener) {
ellipsizeListeners.remove(listener);
}

public boolean isEllipsized() {
return isEllipsized;
}

@Override
public void setMaxLines(int maxLines) {
super.setMaxLines(maxLines);
this.maxLines = maxLines;
isStale = true;
}

public int getMaxLines() {
return maxLines;
}

@Override
public void setLineSpacing(float add, float mult) {
this.lineAdditionalVerticalPadding = add;
this.lineSpacingMultiplier = mult;
super.setLineSpacing(add, mult);
}

@Override
protected void onTextChanged(CharSequence text, int start, int before,
int after) {
super.onTextChanged(text, start, before, after);
if (!programmaticChange) {
fullText = text.toString();
isStale = true;
}
}

@Override
protected void onDraw(Canvas canvas) {
if (isStale) {
super.setEllipsize(null);
resetText();
}
super.onDraw(canvas);
}

private void resetText() {
int maxLines = getMaxLines();
String workingText = fullText;
System.out.println("fullText =" + fullText);
boolean ellipsized = false;
if (maxLines != -1) {
Layout layout = createWorkingLayout(workingText);
if (layout.getLineCount() > maxLines) {

System.out.println("layout line count =" + layout.getLineCount() + "t maxLines =" + maxLines);
workingText = fullText.substring(0,(layout.getLineEnd(maxLines - 1)-1)).trim();
Layout layout2 = createWorkingLayout(workingText + ELLIPSIS);
while (layout2.getLineCount() > maxLines) {
System.out.println("layout2 line count =" + layout2.getLineCount() + "t maxLines =" + maxLines);
int lastSpace = workingText.lastIndexOf(' ');
System.out.println("lastSpace =" + lastSpace);
if (lastSpace == -1) {
break;
}
workingText = workingText.substring(0, lastSpace);
}
workingText = workingText + ELLIPSIS;
System.out.println("workingText =" + workingText);
ellipsized = true;
}
}
if (!workingText.equals(getText())) {
programmaticChange = true;
try {
setText(workingText);
} finally {
programmaticChange = false;
}
}
isStale = false;
if (ellipsized != isEllipsized) {
isEllipsized = ellipsized;
for (EllipsizeListener listener : ellipsizeListeners) {
listener.ellipsizeStateChanged(ellipsized);
}
}
}

private Layout createWorkingLayout(String workingText) {
return new StaticLayout(workingText, getPaint(), getWidth()- getPaddingLeft() - getPaddingRight(), Alignment.ALIGN_NORMAL,
lineSpacingMultiplier, lineAdditionalVerticalPadding, false);
}

@Override
public void setEllipsize(TruncateAt where) {
// Ellipsize settings are not respected } }
}
}

0

  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有