我终于找到了根据textview的高度来调整文本大小的代码。Source,但是我没有得到如何在这个函数中传递文本,我的文本是从另一个意图
public int getHeightOfMultiLineText(String text,int textSize, int maxWidth) {
TextPaint paint = new TextPaint();
paint.setTextSize(textSize);
int index = 0;
int linecount = 0;
while(index < text.length()) {
index += paint.breakText(text,index,text.length(),true,maxWidth,null);
linecount++;
}
Rect bounds = new Rect();
paint.getTextBounds("Yy", 0, 2, bounds);
// obtain space between lines
double lineSpacing = Math.max(0,((linecount - 1) * bounds.height()*0.25));
return (int)Math.floor(lineSpacing + linecount * bounds.height());
}
答案 0 :(得分:0)
您可以使用urTextView.getText().toString()
TextView urTextView;
String text = urTextView.getText().toString();
int textSize = 100;
int maxHeight = 50;
while(getHeightOfMultiLineText(text,textSize,maxWidth) > maxHeight)
textSize--;
这应该有用。