Android 检测ListView中的TextView是否被省略
我有一个自定义适配器,可以在ListView中呈现某些项。如果项目的文本被省略,我需要在ListView的项目上显示一个图标,如果有足够的空间让文本完成,我需要隐藏它。我可以访问适配器的getView方法中的按钮(在这里我设置了文本),但是在设置文本时不会立即添加省略号Android 检测ListView中的TextView是否被省略,android,android-layout,android-listview,textview,Android,Android Layout,Android Listview,Textview,我有一个自定义适配器,可以在ListView中呈现某些项。如果项目的文本被省略,我需要在ListView的项目上显示一个图标,如果有足够的空间让文本完成,我需要隐藏它。我可以访问适配器的getView方法中的按钮(在这里我设置了文本),但是在设置文本时不会立即添加省略号 public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder vh; if (convertView =
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
... // create/inflate view and populate the ViewHolder
}
vh = (ViewHolder) convertView.getTag();
// Set the actual content of the TextView
vh.textView.setText(...);
// Hide the (potentially recycled) expand button until ellipsizing checked
vh.expandBtn.setVisibility(GONE);
Layout layout = vh.textView.getLayout();
if (layout != null) {
// The TextView has already been laid out
// We can check whether it's ellipsized immediately
if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
// Text is ellipsized in re-used view, show 'Expand' button
vh.expandBtn.setVisibility(VISIBLE);
}
} else {
// The TextView hasn't been laid out, so we need to set an observer
// The observer fires once layout's done, when we can check the ellipsizing
ViewTreeObserver vto = vh.textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout layout = vh.textView.getLayout();
if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
// Text is ellipsized in newly created view, show 'Expand' button
vh.expandBtn.setVisibility(VISIBLE);
}
// Remove the now unnecessary observer
// It wouldn't fire again for reused views anyways
ViewTreeObserver obs = vh.textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
return convertView;
}
我有什么办法可以做到这一点吗
以下是我的TextView标记:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:id="@+id/list_item_description"/>
您可以将文本设置为标记。 将此添加到代码中可能会有所帮助
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
您还可以为您的代码放置一个水平滚动视图
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/list_item_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</HorizontalScrollView>
我希望我能正确理解你的问题——如果你想结束一个对于带有省略号的屏幕来说太宽的
文本视图
,你可以将这些属性添加到你的文本视图
:
android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
但是,如果您想确定
TextView
是否以省略号结尾,或者是否完全显示,我不太确定这是可能的——看起来不是这样。不过,您可能还是想尝试TextView
的getEllipsize()
方法。我不确定这是返回Android将TextView省略的点,还是返回您将TextView设置为省略的点。棘手的部分是,您在getView
中使用的视图有时已布局,有时未布局,因此您必须处理这两种情况
如果未布置省略号,则可以设置视图树观察者,以便在省略号布置完成后检查省略号。对于回收视图,布局已经存在,您可以在设置文本后立即检查省略号
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
... // create/inflate view and populate the ViewHolder
}
vh = (ViewHolder) convertView.getTag();
// Set the actual content of the TextView
vh.textView.setText(...);
// Hide the (potentially recycled) expand button until ellipsizing checked
vh.expandBtn.setVisibility(GONE);
Layout layout = vh.textView.getLayout();
if (layout != null) {
// The TextView has already been laid out
// We can check whether it's ellipsized immediately
if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
// Text is ellipsized in re-used view, show 'Expand' button
vh.expandBtn.setVisibility(VISIBLE);
}
} else {
// The TextView hasn't been laid out, so we need to set an observer
// The observer fires once layout's done, when we can check the ellipsizing
ViewTreeObserver vto = vh.textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout layout = vh.textView.getLayout();
if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
// Text is ellipsized in newly created view, show 'Expand' button
vh.expandBtn.setVisibility(VISIBLE);
}
// Remove the now unnecessary observer
// It wouldn't fire again for reused views anyways
ViewTreeObserver obs = vh.textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
return convertView;
}
:
返回要省略号的字符数,如果不进行省略号,则返回0
因此,简单地说:
if(textview1.getLayout().getEllipsisCount() > 0) {
// Do anything here..
}
由于在设置布局之前无法调用getLayout(),请使用ViewTreeObserver查找何时加载textview:
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");
}
}
});
最后,当您需要删除时,请不要忘记删除removeOnGlobalLayoutListener。您为省略号做了什么?您是否尝试了。添加了与我的文本视图相关的部分。您的建议都没有解决Hadi提出的问题,这就是为什么省略号属性没有显示预期的行为。相反,它们解决了一个不同的问题(而
水平滚动视图
方案尤其糟糕)。我们检查已经布置好的文本视图的部分似乎不起作用。在Android仿真器api 23上测试。