//以api30为例privatestaticintdesired(Layout layout){int n = layout.getLineCount();CharSequence text = layout.getText();float max =0;// if any line was wrapped, we can't use it.// but it's ok for the last line not to have a newlinefor(int i =0; i < n -1; i++){if(text.charAt(layout.getLineEnd(i)-1)!='\n'){return-1;}}for(int i =0; i < n; i++){//关键就是这一句
max =Math.max(max, layout.getLineWidth(i));}return(int)Math.ceil(max);}
//api31privatestaticintdesired(Layout layout){int n = layout.getLineCount();CharSequence text = layout.getText();float max =0;// if any line was wrapped, we can't use it.// but it's ok for the last line not to have a newlinefor(int i =0; i < n -1; i++){if(text.charAt(layout.getLineEnd(i)-1)!='\n'){return-1;}}for(int i =0; i < n; i++){//关键就是这一句
max =Math.max(max, layout.getLineMax(i));}return(int)Math.ceil(max);}