测量多行textview的每一行的宽度

时间:2014-11-06 08:51:01

标签: android textview measure

我有一个带有多行文字的文本视图,我想测量每行的像素宽度。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

String[] line = textView.getText().toString().split("\n");
ArrayList<Float> lineWidths = new ArrayList<Float>();
float textViewWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
for (int i = 0; i < line.length; i++) {
    float width = textView.getPaint().measureText(line[i]);
    if (width <= textViewWidth) {
        lineWidths.add(width);
    } else {
        int m = (int) (width / textViewWidth);
        for (int j = 0; j < m; j++) {
            lineWidths.add(textViewWidth);
        }
        lineWidths.add(width - textViewWidth * m);
    }
}