I have a multi-line TextView
that has android:ellipsize="end"
set. I would like to know, however, if the string I place in there is actually too long (so that I may make sure the full string is shown elsewhere on the page).
我有一个多行文本视图,它有android:省略号="end"设置。但是我想知道,如果我放在那里的字符串实际上太长了(这样我可以确保整个字符串显示在页面的其他地方)。
I could use TextView.length()
and find about what the approximate length of string will fit, but since it's multiple lines, the TextView
handles when to wrap, so this won't always work.
我可以使用TextView.length()来查找字符串的长度,但是由于它是多行,所以TextView会处理什么时候换行,所以这并不总是有效。
Any ideas?
什么好主意吗?
9 个解决方案
#1
97
You can get the layout of the TextView
and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:
您可以获得TextView的布局并检查每行的省略号计数。对于结束省略号,只需检查最后一行,如下所示:
Layout l = textview.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.
这只适用于布局阶段之后,否则返回的布局将为空,因此在代码中的适当位置调用它。
#2
23
textView.getLayout is the way to go but the problem with that is that it returns null if layout is not prepared. Use the below solution.
textView。getLayout是一种方法,但问题是如果布局没有准备好,它会返回null。使用下面的解决方案。
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = textview.getLayout();
if ( l != null){
int lines = l.getLineCount();
if ( lines > 0)
if ( l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
}
});
#3
10
I think the easiest solution to this question is the following code:
我认为这个问题最简单的解决方法是下面的代码:
String text = "some looooong text";
textView.setText(text);
boolean isEllipsize = !((textView.getLayout().getText().toString()).equalsIgnoreCase(text));
This code assumes that in your XML the TextView set a maxLineCount
:)
这段代码假设在XML中TextView设置了maxLineCount:)
#4
3
public int getEllipsisCount (int line):
公共int get椭圆体(int line):
Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.
返回要省掉的字符数,如果没有省略号,则返回0。
So, simply call :
所以,简单地调用:
int lineCount = textview1.getLineCount();
if(textview1.getLayout().getEllipsisCount(lineCount) > 0) {
// Do anything here..
}
Since the getLayout() cant be called before the layout is set, use this:
由于在设置布局之前不能调用getLayout(),请使用以下命令:
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = textview.getLayout();
if ( l != null){
int lines = l.getLineCount();
if ( lines > 0)
if ( l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
}
});
And finally do not forget to remove removeOnGlobalLayoutListener when you need it nomore.
最后,当你需要的时候,不要忘记删除removeloballayoutlistener。
#5
2
I've created a custom TextView with a listener notification for this very problem. It also fixes the multi-line ellipse problem. You can find it posted at android ellipsize multiline textview
我已经为这个问题创建了一个自定义TextView和侦听器通知。它还解决了多行椭圆问题。你可以在android省略号的multiline textview上找到它
#6
0
it is working for me
这对我很有效
if (l != null) {
int lines = l.getLineCount();
if (lines > 0) {
for (int i = 0; i < lines; i++) {
if (l.getEllipsisCount(i) > 0) {
ellipsize = true;
break;
}
}
}
}
#7
0
Really work so, for example, to pass full data to dialog from item of RecyclerView:
例如,真正的工作是将完整的数据从回收的项目中传递给对话框:
holder.subInfo.post(new Runnable() {
@Override
public void run() {
Layout l = holder.subInfo.getLayout();
if (l != null) {
final int count = l.getLineCount();
if (count >= 3) {
holder.subInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final int c = holder.subInfo.getLineCount();
if (c >= 3) {
onClickToShowInfoDialog.showDialog(holder.title.getText().toString(), holder.subInfo.getText().toString());
}
}
});
}
}
}
});
#8
-1
Using getEllipsisCount
won't work with text that has empty lines within it. I used the following code to make it work :
使用get椭圆体scount不能处理包含空行的文本。我使用下面的代码使它工作:
message.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if(m.isEllipsized == -1) {
Layout l = message.getLayout();
if (message.getLineCount() > 5) {
m.isEllipsized = 1;
message.setMaxLines(5);
return false;
} else {
m.isEllipsized = 0;
}
}
return true;
}
});
Make sure not to set a maxLineCount
in your XML. Then you can check for the lineCount
in your code and if it is greater than a certain number, you can return false to cancel the drawing of the TextView
and set the line count as well as a flag to save whether the text view is too long or not. The text view will draw again with the correct line count and you will know whether its ellipsized or not with the flag.
确保不要在XML中设置maxLineCount。lineCount的你可以检查你的代码,如果大于一定数量,你可以返回false取消的TextView并设置行数以及国旗保存文本视图是否太长了。文本视图将使用正确的行数再次绘制,您将知道它的椭圆大小是否带有标志。
You can then use the isEllipsized
flag to do whatever you require.
然后,您可以使用is椭圆大小的标志来执行您需要的任何操作。
#9
-1
create a method inside your TextViewUtils class
在TextViewUtils类中创建一个方法
public static boolean isEllipsized(String newValue, String oldValue) {
return !((newValue).equals(oldValue));
}
call this method when it's required eg:
在需要的时候调用这个方法例如:
if (TextViewUtils.isEllipsized(textviewDescription.getLayout().getText().toString(), yourModelObject.getDescription()))
holder.viewMore.setVisibility(View.VISIBLE);//show view more option
else
holder.viewMore.setVisibility(View.GONE);//hide
but textView.getLayout() can't call before the view(layout) set.
但是textView.getLayout()不能在视图(layout)设置之前调用。
#1
97
You can get the layout of the TextView
and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:
您可以获得TextView的布局并检查每行的省略号计数。对于结束省略号,只需检查最后一行,如下所示:
Layout l = textview.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.
这只适用于布局阶段之后,否则返回的布局将为空,因此在代码中的适当位置调用它。
#2
23
textView.getLayout is the way to go but the problem with that is that it returns null if layout is not prepared. Use the below solution.
textView。getLayout是一种方法,但问题是如果布局没有准备好,它会返回null。使用下面的解决方案。
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = textview.getLayout();
if ( l != null){
int lines = l.getLineCount();
if ( lines > 0)
if ( l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
}
});
#3
10
I think the easiest solution to this question is the following code:
我认为这个问题最简单的解决方法是下面的代码:
String text = "some looooong text";
textView.setText(text);
boolean isEllipsize = !((textView.getLayout().getText().toString()).equalsIgnoreCase(text));
This code assumes that in your XML the TextView set a maxLineCount
:)
这段代码假设在XML中TextView设置了maxLineCount:)
#4
3
public int getEllipsisCount (int line):
公共int get椭圆体(int line):
Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.
返回要省掉的字符数,如果没有省略号,则返回0。
So, simply call :
所以,简单地调用:
int lineCount = textview1.getLineCount();
if(textview1.getLayout().getEllipsisCount(lineCount) > 0) {
// Do anything here..
}
Since the getLayout() cant be called before the layout is set, use this:
由于在设置布局之前不能调用getLayout(),请使用以下命令:
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = textview.getLayout();
if ( l != null){
int lines = l.getLineCount();
if ( lines > 0)
if ( l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
}
});
And finally do not forget to remove removeOnGlobalLayoutListener when you need it nomore.
最后,当你需要的时候,不要忘记删除removeloballayoutlistener。
#5
2
I've created a custom TextView with a listener notification for this very problem. It also fixes the multi-line ellipse problem. You can find it posted at android ellipsize multiline textview
我已经为这个问题创建了一个自定义TextView和侦听器通知。它还解决了多行椭圆问题。你可以在android省略号的multiline textview上找到它
#6
0
it is working for me
这对我很有效
if (l != null) {
int lines = l.getLineCount();
if (lines > 0) {
for (int i = 0; i < lines; i++) {
if (l.getEllipsisCount(i) > 0) {
ellipsize = true;
break;
}
}
}
}
#7
0
Really work so, for example, to pass full data to dialog from item of RecyclerView:
例如,真正的工作是将完整的数据从回收的项目中传递给对话框:
holder.subInfo.post(new Runnable() {
@Override
public void run() {
Layout l = holder.subInfo.getLayout();
if (l != null) {
final int count = l.getLineCount();
if (count >= 3) {
holder.subInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final int c = holder.subInfo.getLineCount();
if (c >= 3) {
onClickToShowInfoDialog.showDialog(holder.title.getText().toString(), holder.subInfo.getText().toString());
}
}
});
}
}
}
});
#8
-1
Using getEllipsisCount
won't work with text that has empty lines within it. I used the following code to make it work :
使用get椭圆体scount不能处理包含空行的文本。我使用下面的代码使它工作:
message.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if(m.isEllipsized == -1) {
Layout l = message.getLayout();
if (message.getLineCount() > 5) {
m.isEllipsized = 1;
message.setMaxLines(5);
return false;
} else {
m.isEllipsized = 0;
}
}
return true;
}
});
Make sure not to set a maxLineCount
in your XML. Then you can check for the lineCount
in your code and if it is greater than a certain number, you can return false to cancel the drawing of the TextView
and set the line count as well as a flag to save whether the text view is too long or not. The text view will draw again with the correct line count and you will know whether its ellipsized or not with the flag.
确保不要在XML中设置maxLineCount。lineCount的你可以检查你的代码,如果大于一定数量,你可以返回false取消的TextView并设置行数以及国旗保存文本视图是否太长了。文本视图将使用正确的行数再次绘制,您将知道它的椭圆大小是否带有标志。
You can then use the isEllipsized
flag to do whatever you require.
然后,您可以使用is椭圆大小的标志来执行您需要的任何操作。
#9
-1
create a method inside your TextViewUtils class
在TextViewUtils类中创建一个方法
public static boolean isEllipsized(String newValue, String oldValue) {
return !((newValue).equals(oldValue));
}
call this method when it's required eg:
在需要的时候调用这个方法例如:
if (TextViewUtils.isEllipsized(textviewDescription.getLayout().getText().toString(), yourModelObject.getDescription()))
holder.viewMore.setVisibility(View.VISIBLE);//show view more option
else
holder.viewMore.setVisibility(View.GONE);//hide
but textView.getLayout() can't call before the view(layout) set.
但是textView.getLayout()不能在视图(layout)设置之前调用。