0

我想做这样的事情......

两个 TextViews,第一个多行,最后我需要另一个 textview

|t1 content t1 content t1 content     |
|t1 content t1 content... [t2 content]|

With shorter content

|t1 content t1 content t1 content     |
|t1 content               [t2 content]|

它类似于这里回答的问题 两个 TextViews并排,只有一个椭圆? ...但我需要多线解决方案

有任何想法吗?

4

1 回答 1

0

尝试这个

我创建了这个全局函数,在你想要椭圆结束之后,你必须传递 textview 对象和行数。

public void doEllipsize(final TextView tv, final int maxLine) {
        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {

                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (maxLine <= 0) {
                    int lineEndIndex = tv.getLayout().getLineEnd(0);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                } else if (tv.getLineCount() >= maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                }
            }
        });
    }
于 2013-09-05T07:20:10.290 回答