我有一个带有多行文字的文本视图,我想测量每行的像素宽度。有没有办法做到这一点?
答案 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);
}
}