Android 如何同时设置TextView的maxLines和ellipsize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2909424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:50:59  来源:igfitidea点击:

How to set maxLines and ellipsize of a TextView at the same time

android

提问by michael

I want to limit my text view to have maximum of 5 lines, so I did:

我想将我的文本视图限制为最多 5 行,所以我做了:

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    android:maxLines="5" />

But when I try to configure it to add '...' when the text is truncated, I add android:ellipsize="end". I do see the ... but then my TextView only has a max line of 2, instead of 5.

但是当我尝试将其配置为在文本被截断时添加 '...' 时,我添加了 android:ellipsize="end"。我确实看到了...但是我的 TextView 最多只有 2 行,而不是 5 行。

Can you please suggest how can I make the text view of maximum line of 5 and add '...' when it get truncated?

您能否建议我如何制作最多 5 行的文本视图并在它被截断时添加“...”?

Thank you.

谢谢你。

回答by Imeshke

Progrmatically, you can use:

以编程方式,您可以使用:

your_text_view.setEllipsize(TextUtils.TruncateAt.END);
your_text_view.setMaxLines(4);
your_text_view.setText("text");  

回答by Max M.

try this code...

试试这个代码...

    final TextView tv_yourtext = (TextView)findViewById(R.id.text);

    tv_yourtext.setText("A really long text");
    ViewTreeObserver vto = tv_yourtext.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            ViewTreeObserver obs = tv_yourtext.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
            if(tv_yourtext.getLineCount() > 6){
                Log.d("","Line["+tv_yourtext.getLineCount()+"]"+tv_yourtext.getText());
                int lineEndIndex = tv_yourtext.getLayout().getLineEnd(5);
                String text = tv_yourtext.getText().subSequence(0, lineEndIndex-3)+"...";
                tv_yourtext.setText(text);
                Log.d("","NewText:"+text);
            }

        }
    });

回答by John Alexander Betts

You only need to add this line to your TextView:

您只需要将此行添加到您的 TextView:

android:ellipsize="marquee"

回答by SeptimusX75

Apparently you have to specify the inputTypeand set it to nonein this case. It should look something like this:

显然inputTypenone在这种情况下,您必须指定并将其设置为。它应该是这样的:

<TextView
  android:ellipsize="end"
  android:inputType="none"
  android:maxLines="2"

回答by Lope

There is same question android ellipsize multiline textviewThis is known bug, quite old and not fixed yet :(

有同样的问题android ellipsize multiline textview这是一个已知的错误,很老而且还没有修复:(

someone posted workaround http://code.google.com/p/android-textview-multiline-ellipse/maybe it will help you (or someone else)

有人发布了解决方法http://code.google.com/p/android-textview-multiline-ellipse/也许它会帮助你(或其他人)

回答by Bilal Ameer

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"  
android:maxLines="5"
android:ellipsize="end" />

Works for me, make sure TextView top container should have height like

对我有用,确保 TextView 顶部容器的高度应该像

android:layout_height="wrap_content"

回答by david72

When I set the TextView height to a constant value (like 50dp), it works fine for me.

当我将 TextView 高度设置为常量值(如 50dp)时,它对我来说效果很好。