代码计算TextView是单行还是多行

    String mDisplayContent = "aaaaaaaaa";
    TextPaint textPaint = new TextPaint();
    textPaint.setTextSize(getResources().getDimension(R.dimen.font_size8));
    DisplayMetrics dm = getResources().getDisplayMetrics();
    int viewWidth = dm.widthPixels - contentTv.getPaddingLeft() - contentTv.getPaddingRight();
    //用DynamicLayout,在TextView未绘制的情况下,计算textView行数
    DynamicLayout textLayout = new DynamicLayout(mDisplayContent,
            textPaint, viewWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
    if (textLayout.getLineCount() > 1) {
        contentTv.setGravity(Gravity.CENTER_VERTICAL);
    }

你可能感兴趣的:(代码计算TextView是单行还是多行)