检查TextView的是Android ellipsized [英] Check if textview is ellipsized in android
问题描述
我的TextView
与宽度包装内容
。在这种的TextView
我设置文本,但每次文字是相同长度的不行。当文字很长,我用单行真实和 ellipsize
:结束。但现在我有一个问题。我想设置的其他布局可见,但是依赖于长我的文字。如果文本太长,以适应我要调用setVisible真实画面,但是当文字很短,当我不需要ellipsize,我想设置知名度假。所以,我需要检查我的TextView的状态。当其ellipsize我想调用setVisible
true,则它不是调用setVisible
假的。我怎样才能做到这一点。
这是我得到了什么:
I have TextView
with width as wrap content
. In this TextView
I set text, but text is not of the same length every time. When text is very long I use single line true and ellipsize
: end. But now I have a problem. I want to set Visibility of other layout but that depends on the length my text. If text is too long to fit in the screen I want to setVisible true, but when text is short and when I don't need ellipsize, I want to set visibility false. So I need to check status of my TextView. When its ellipsize I want to setVisible
true, when its not setVisible
false. How I can do that.
This is what I got:
tvAle.post(new Runnable() {
@Override
public void run() {
int lineCount = tvAle.getLineCount();
Paint paint = new Paint();
paint.setTextSize(tvAle.getTextSize());
final float size = paint.measureText(tvAle.getText().toString());
Log.v("a", ""+size+" "+tvAle.getWidth());
if ((int)size > (tvAle.getWidth()+10)) {
allergiesLayout.setVisibility(View.VISIBLE);
}
else
allergiesLayout.setVisibility(View.GONE);
}
但是这种解决方案是行不通的。
but this solution doesn't work.
推荐答案
您可以使用提供此方法:<一href=\"http://developer.android.com/reference/android/text/StaticLayout.html#getEllipsisCount%28int%29\">getEllipsisCount
You can use this method provided: getEllipsisCount
Layout layout = textview1.getLayout();
if(layout != null) {
int lines = layout.getLineCount();
if(lines > 0) {
int ellipsisCount = layout.getEllipsisCount(lines-1);
if ( ellipsisCount > 0) {
Log.d(TAG, "Text is ellipsized");
}
}
}
在这里可通过getLineCount得到线()
where line could be obtained via getLineCount()
这篇关于检查TextView的是Android ellipsized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!