我有一个包含多行文本视图的Android应用程序布局。当屏幕处于纵向方向时,文本视图可以显示不同长度的文本,而在横向模式下运行时,则可显示不同长度的文本。此外,在小屏幕上运行时,文本视图可以显示不同长度的文本,而在大屏幕上则不同。
有没有办法检查文本是否适合或将被截断?或者有没有办法检查TextView是否已满?
问题是TextView可能包含不同数量的行,这取决于它是横向、纵向、小屏幕、大屏幕等。
感谢您的建议,
最好的问候,
詹姆斯
我有一个包含多行文本视图的Android应用程序布局。当屏幕处于纵向方向时,文本视图可以显示不同长度的文本,而在横向模式下运行时,则可显示不同长度的文本。此外,在小屏幕上运行时,文本视图可以显示不同长度的文本,而在大屏幕上则不同。
有没有办法检查文本是否适合或将被截断?或者有没有办法检查TextView是否已满?
问题是TextView可能包含不同数量的行,这取决于它是横向、纵向、小屏幕、大屏幕等。
感谢您的建议,
最好的问候,
詹姆斯
这些答案对我来说并不是很有效。这是我最终采取的做法
Paint measurePaint = new Paint(myTextView.getPaint());
float pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?");
float labelWidth = myTextView.getWidth();
int maxLines = myTextView.getMaxLines();
while (labelWidth > 0 && pWidth/maxLines > labelWidth-20) {
float textSize = measurePaint.getTextSize();
measurePaint.setTextSize(textSize-1);
pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?");
if (textSize < TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 7,
getContext().getResources().getDisplayMetrics())) break;
}
myTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, measurePaint.getTextSize());
我并不是说这种方法适用于每一种情况,因为我确实在偷懒,但是总体的思路是使用TextView的paint来测量文本,然后不断缩小文本直到它适合TextView为止。
我找到了一个"狡猾的"解决办法,用于测量多行TextView中文本的高度:-
//Calculate the height of the text in the MULTILINE TextView
int textHeight = textView.getLineCount() * textView.getLineHeight();
if (textHeight > textViewHeight) {
//Text is truncated because text height is taller than TextView height
} else {
//Text not truncated because text height not taller than TextView height
}
然而,这个解决方案有一些注意点:
首先,关于getLineHeight(),文本内的标记可能会导致单独的行比该高度更高或更短,并且布局可能包含额外的首行或末行填充。请参阅http://developer.android.com/reference/android/widget/TextView.html#getLineHeight()。
其次,应用程序需要计算TextView在像素中的实际高度,并且(对于我的布局)它可能不像textView.getHeight()那样简单,而是因布局而异。
我建议避免使用LinearLayout,因为TextView的实际像素高度可能会根据文本内容而变化。我正在使用RelativeLayout (见http://pastebin.com/KPzw5LYd)。
使用此RelativeLayout,我可以按以下方式计算我的TextView高度:
//Calculate the height of the TextView for layout "http://pastebin.com/KPzw5LYd"
int textViewHeight = layout1.getHeight() - button1.getHeight() - button2.getHeight();
希望有所帮助,
敬礼,
詹姆斯
基本上,当屏幕方向改变时,您需要计算您的文本视图和文本的大小。尝试使用ViewTreeObserver.OnGlobalLayoutListener
来实现。
在更改方向的方法内部:
main_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
main_view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//Calculation goes here
int text_size = getTextSize(text_view.getText().toString());
int text_view_size = text_view.getLayoutParams().width;
//Compare your text_size and text_view_size and do whatever you want here.
}
});
private int getTextSize(String your_text){
Paint p = new Paint();
//Calculate the text size in pixel
return p.measureText(your_text);
}
这个 Kotlin 的扩展函数对我很有用,只需要确保在视图已经布局完成后调用它,例如 view.post()
是一个不错的位置;
/**
* Make sure to call this method only when view layout is finished, e.g. view.post() is a good place to check this
*/
fun TextView.isTruncated() = (lineCount * lineHeight) > height
使用方法
textView.post {
if(isTruncated()) {
// Do something
}
}
如果要检查多行(或非多行)TextView是否将被截断,请参考this post。
或者,您是否考虑使用滚动文本视图?(跑马灯)..如果文本对于给定的宽度太长,则文本将通过水平滚动(动画方式)滚动?
这是一个示例TextView在布局文件中,具有一些这些特征:
<TextView
android:id="@+id/sometextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:textColor="#808080"
android:textSize="14sp"
android:text="This is a long scrolling line of text.. (etc)"/>