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

Android 如何同时设置TextView的maxLines和ellipsize,android,Android,我想将我的文本视图限制为最多5行,因此我做到了: <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="5" /> 但当我试图将其配置为在文本被截断时添加“…”时,我添加了android:ellipsize=“end”。我确实看到了。。。但是我的文本视图

我想将我的文本视图限制为最多5行,因此我做到了:

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

但当我试图将其配置为在文本被截断时添加“…”时,我添加了android:ellipsize=“end”。我确实看到了。。。但是我的文本视图的最大行数是2,而不是5

您能建议我如何使文本视图的最大行为5,并在其被截断时添加“…”吗

谢谢。

还有一个相同的问题,这是已知的错误,非常旧,尚未修复:(

有人发布了解决方案,也许它会帮助你(或其他人)

试试这段代码

    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);
            }

        }
    });

您只需将此行添加到TextView:

android:ellipsize="marquee"

从程序上讲,您可以使用:

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

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

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

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

你明白了吗?也许你可以在下面发布你的答案供其他用户参考。删除这行:android:ellipsize=“…”对我来说,删除
省略
时,它确实有效。我使用
设置垂直渐变边启用
设置渐变边长度
。你是如何解决的?@Michaelth此代码对我非常有效!@michael,如果对你有效,你应该将其标记为答案。我有一个问题。follo的功能是什么wing code.ViewTreeObserver obs=tv_yourtext.getViewTreeObserver();obs.removeglobalonlyoutliner(此);我可以使用vto.removeglobalonlyoutliner(此)取而代之?谢谢。使用
RecyclerView.Adapter TextView
不设置文本检查我的问题这对我不起作用它总是设置为1行检查您的xml文件。可能是您的TextView可以有android:maxLines=“1”。如果它是删除它。对我来说,它不存在。Android中存在一个明确的缺陷。如果我使用treeObserver方法。当我最多需要2行,并且在最后使用“…”时,它对我不起作用
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"  
android:maxLines="5"
android:ellipsize="end" />
android:layout_height="wrap_content"