I'm having a hard time reseting the maxLines attribute of a TextView programmatically.
我很難以編程方式重新設置TextView的maxLines屬性。
Just tried setting to 0 and it doesn't work. -1 crashes the application. I could use a simpler workaround and set the maxLines to 5000 but I don't want to do that.
剛嘗試設置為0,它不起作用。 -1使應用程序崩潰。我可以使用更簡單的解決方法並將maxLines設置為5000但我不想這樣做。
Any ideas how to do that?
任何想法如何做到這一點?
UPDATED
更新
Well, I've found one problem.. I've set the Ellipsize as well... I'm just going to use the following workaround:
好吧,我發現了一個問題..我也設置了Ellipsize ......我將使用以下解決方法:
TextView questionDetail = (TextView) mQuestionDetailHeader.findViewById(R.id.desc);
questionDetail.setText(mCurrentQuestion.getQuestion());
questionDetail.setMaxLines(Integer.MAX_VALUE); //As in the android sourcecode
questionDetail.setEllipsize(null);
4 个解决方案
#1
49
As there isn't yet an approved answer - the proper way to reset the maxlines property of the TextView is:
由於還沒有批准的答案 - 重置TextView的maxlines屬性的正確方法是:
textView.setMaxLines(Integer.MAX_VALUE);
textView.setMaxLines(Integer.MAX_VALUE的);
As per Valdemar's comment and this stackoverflow answer. Using -1 will cause an ArrayIndexOutOfBoundsException.
根據Valdemar的評論和這個stackoverflow的答案。使用-1將導致ArrayIndexOutOfBoundsException。
Keep in mind only END and MARQEE setEllipsize() settings will be respected for maxlines >= 2 according to the documentation:
請記住,根據文檔,只有END和MARQEE setEllipsize()設置將遵循maxlines> = 2:
If setMaxLines(int) has been used to set two or more lines, END and MARQUEE* are only supported (other ellipsizing types will not do anything).
如果setMaxLines(int)用於設置兩行或更多行,則僅支持END和MARQUEE *(其他ellipsizing類型將不執行任何操作)。
#2
0
The -1 should not crash your application. This actually what is used inside TextView by default:
-1不應該使您的應用程序崩潰。這實際上是默認情況下在TextView中使用的內容:
case com.android.internal.R.styleable.TextView_maxLines:
setMaxLines(a.getInt(attr, -1));
break;
This piece of code shows, that when android:maxLines is not specified, then code uses -1 to set the value via setMaxLines() function.
這段代碼顯示,當未指定android:maxLines時,代碼使用-1通過setMaxLines()函數設置值。
I also made test application to verify my conclusions. And it works fine without crashing:
我也做了測試申請來驗證我的結論。它工作正常而不會崩潰:
public class HelloWorld extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
TextView text = (TextView)findViewById(R.id.text);
text.setMaxLines(-1);
}
}
#3
-1
For setting the maxLines for a text use mTextView.setMaxLines(0) or you have to programmatic-ally measure of the height text and multiply with the number of max line The result should set as the height of the textView
要設置文本的maxLines,請使用mTextView.setMaxLines(0),或者必須以編程方式測量高度文本並乘以最大行數。結果應設置為textView的高度
#4
-2
if you want to have just a single line , then why don't you use:
如果你想只有一行,那你為什么不使用:
txtView.setSingleLine(true);