TextView最后一行,不完全显示。截取显示

来源:互联网 发布:单链表实现java 编辑:程序博客网 时间:2024/06/08 04:08

1、效果图大致如下:


本代码存在的弊端是存放在listView中,可能出现需要滑动才可以刷新,设置文本。 

可以使用:https://code.google.com/p/android-textview-multiline-ellipse/source/browse/

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.TextPaint;import android.text.TextUtils;import android.util.AttributeSet;import android.util.TypedValue;import android.view.ViewTreeObserver;import android.widget.TextView;import com.xingtuan.hysd.R;/** * TextView的最后一行,不完全显示文本。 * Created by jiangp on 15/4/19. */public class AutoTextView extends TextView {    private int mEmptyWidth = 150;//空白文本宽度    private int mMinLine = 2;    public AutoTextView(Context context) {        this(context, null);    }    public AutoTextView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AutoTextView);        int emptyWidth = (int) ta.getDimension(R.styleable.AutoTextView_atv_empty_width, 0);        ta.recycle();        if (emptyWidth > 0) {            mEmptyWidth = emptyWidth;        } else {            mEmptyWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mEmptyWidth, context.getResources().getDisplayMetrics());        }    }    public void setAutoText(final CharSequence text) {        if (TextUtils.isEmpty(text)) {            return;        }        this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                //可显示文本区域的宽度                int availableTextWidth = mMinLine * (getWidth() - getPaddingLeft() - getPaddingRight()) - mEmptyWidth;                Paint paint = getPaint();                paint.setTextSize(getTextSize());                // 根据长度截取出剪裁后的文字                String ellipsizeStr = (String) TextUtils.ellipsize(text, (TextPaint) paint, availableTextWidth, TextUtils.TruncateAt.END);                setText(ellipsizeStr);            }        });    }}


0 0
原创粉丝点击