在我的应用程序中,我有一个屏幕,其中显示一些文本,然后显示一张照片。文本的长度是可变的(有时根本没有,有时很多),所以我想对其进行设置,以便文本不会占用超过几行(但可以滚动),从而为下面的图像留出足够的空间。
我的这部分 View 组件是通过编程方式创建的,我调整了代码以具有以下内容(目前在我的文本设置方法中,但如果它在初始 View 创建代码中也会发生同样的事情)
public void SetDescription(String description)
{
mTxtDescription.setText(Html.fromHtml(description));
mTxtDescription.setClickable(true);
mTxtDescription.setMaxLines(5);
mTxtDescription.setLines(5); //this makes no difference either!
mTxtDescription.setSingleLine(false);
mTxtDescription.setScrollbarFadingEnabled(true);
mTxtDescription.setScrollBarStyle(VERTICAL);
mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
mTxtDescription.invalidate(); //adding this made no difference...
}
但是它不起作用 - 长文本仍然填满整个屏幕,并且图像由于被向下推到 0 的高度而消失了。我怎样才能让文本永远不超过 5 行?
最佳答案
尝试删除对 setSingleLine
的调用。并使用 setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
。它还会将此调用放在 setMaxLines
和 setLines
调用之前以确保。
注意:setLines
覆盖了 setMaxLines
和 setMinLines
的设置。
TextView
围绕如何显示多个、省略号等的各种调用有很多问题。
关于android - TextView.setMaxLines 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11408922/