检测TextView的ListView中是否ellipsized [英] Detect whether TextView in ListView is ellipsized
问题描述
我有一个自定义适配器呈现在ListView一些项目。我需要显示在ListView的项目的图标,如果项目的文本ellipsized,并隐藏它,如果有足够的空间用于文本来完成。我有机会在我的适配器的getView方法按钮(在这里我设置文本),但椭圆不会立即在设置文本补充说。
有没有什么办法可以做到这一点?
下面是我的TextView的标记:
< TextView的Android的:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:ellipsize =结束
机器人:单线=真
机器人:ID =@ + ID / list_item_description/>
<一个href="http://developer.android.com/reference/android/text/StaticLayout.html#getEllipsisCount%28int%29">public INT getEllipsisCount(INT线):
返回要ellipsized字符数以外,或0,如果没有省略号要发生
因此,只需拨打:
如果(textview1.getLayout()getEllipsisCount()0){
//这里做什么..
}
由于getLayout()不能被称为布局设置之前,使用ViewTreeObserver发现当TextView的加载:
ViewTreeObserver VTO = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
布局升= textview.getLayout();
如果(L!= NULL){
诠释行= l.getLineCount();
如果(线大于0)
如果(l.getEllipsisCount(线-1)大于0)
Log.d(TAG,文本ellipsized);
}
}
});
最后不要忘记删除 removeOnGlobalLayoutListener 当你曲子需要它。
I have a custom Adapter that renders some items in a ListView. I need to show an icon on the ListView's items, if the item's text is ellipsized, and hide it if there's enough room for the text to finish. I have access to the button in getView method of my adapter (where I set the text) but the ellipses are not added immediately upon setting the text.
Is there any way I can do this?
Here's my TextView markup:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:id="@+id/list_item_description"/>
public int getEllipsisCount (int line):
Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.
So, simply call :
if(textview1.getLayout().getEllipsisCount() > 0) {
// Do anything here..
}
Since the getLayout() cant be called before the layout is set, use ViewTreeObserver to find when the textview is loaded:
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.
这篇关于检测TextView的ListView中是否ellipsized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!