重写TextView解决HorizontalScrollView中TextView显示不全的问题

来源:互联网 发布:matlab把向量变成矩阵 编辑:程序博客网 时间:2024/05/22 03:39
/**
 * 适用于宽度相对确定的情况 * Created by xieyuhai on 2017/9/13. */
public class MyTextView extends AppCompatTextView {    private static final String TAG = "MyTextView";    public MyTextView(Context context) {        this(context, null);    }    public MyTextView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onFinishInflate() {        super.onFinishInflate();        Log.e(TAG, "onFinishInflate: w:" + getWidth() + "; height:" + getHeight());    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);
       float textWidth = getPaint().measureText(getText().toString());        BigDecimal v1 = new BigDecimal(Double.toString(textWidth));        BigDecimal v2 = new BigDecimal(Double.toString(w));//一行的高度*总行数+上下内边距= 控件的高度             避免死循环,保留两位小数,考虑到精度可能会丢失,在后面加1        BigDecimal lineCount = v1.divide(v2, 2, BigDecimal.ROUND_HALF_EVEN).add(new BigDecimal(1));        Paint.FontMetrics fm = getPaint().getFontMetrics();        double v = Math.ceil(fm.descent - fm.ascent);//
//一行的高度*总行数+上下内边距= 控件的高度
setHeight((int) (v * lineCount.doubleValue()) + getPaddingTop() + getPaddingBottom());

}

}

阅读全文
0 0
原创粉丝点击