固定宽度,测量一段文字的高度

public static int measureTextViewHeight(CharSequence text,int width,int textSize,float lineSpacingRatio,
            int paddingLeft,int paddingTop,int paddingRight,int paddingBottom){
        TextView textView = new TextView(MyApplication.getInstance());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        textView.setLineSpacing(0, lineSpacingRatio);

        textView.setPadding(CommonUtils.dip2px(paddingLeft), CommonUtils.dip2px(paddingTop),
                CommonUtils.dip2px(paddingRight), CommonUtils.dip2px(paddingBottom));
        textView.setTextSize(textSize);
        params.width = width;
        textView.setLayoutParams(params);
        textView.setText(text);

        int freeSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);

        textView.measure(widthSpec, freeSpec);
        int viewheight = textView.getMeasuredHeight();
        XLogUtil.d("measureTextViewHeight:"+viewheight);
        return viewheight;
    }

你可能感兴趣的:(固定宽度,测量一段文字的高度)