android:maxLines与android:ellipsize冲突


http://stackoverflow.com/questions/11210553/setting-ellipsize-on-textview-reduces-lines-shown-by-one-instead-of-only-ellip
TextView snippet;
snippet.setText("loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor ")
ViewTreeObserver vto = this.snippet.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        ViewTreeObserver obs = snippet.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
        if (snippet.getLineCount() > 3) {
            int lineEndIndex = snippet.getLayout().getLineEnd(2);
            String text = snippet.getText().subSequence(0, lineEndIndex - 3) + "...";
            snippet.setText(text);
        }
    }
});

你可能感兴趣的:(Android,杂货铺)