Had*_*ari 14 android textview android-layout android-listview
我有一个自定义适配器,在ListView中呈现一些项目.如果项目的文本是椭圆形的,我需要在ListView的项目上显示一个图标,如果有足够的空间让文本完成,我需要隐藏它.我可以访问我的适配器的getView方法中的按钮(我设置文本),但是在设置文本时不会立即添加省略号.
有什么方法可以做到这一点吗?
这是我的TextView标记:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:id="@+id/list_item_description"/>
Run Code Online (Sandbox Code Playgroud)
ama*_*Bit 13
public int getEllipsisCount(int line):
返回要椭圆化的字符数,如果不进行省略则返回0.
所以,只需致电:
if(textview1.getLayout().getEllipsisCount() > 0) {
// Do anything here..
}
Run Code Online (Sandbox Code Playgroud)
由于在设置布局之前无法调用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");
}
}
});
Run Code Online (Sandbox Code Playgroud)
最后,当你需要它时,不要忘记删除removeOnGlobalLayoutListener.
棘手的部分是你正在使用的视图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;
}
Run Code Online (Sandbox Code Playgroud)
His*_*eer -1
您可以将文本设置为标记。在您的代码中添加此内容可能会有所帮助......
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
Run Code Online (Sandbox Code Playgroud)
您还可以为您的代码添加水平滚动视图......
<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>
Run Code Online (Sandbox Code Playgroud)