我正在尝试创建一种方法来调整多行文本的大小TextView,使其适合于(在X和Y维度)的范围内TextView.
目前,我有一些东西,但它所做的只是调整文本大小,使文本的第一个字母/字符填充尺寸TextView(即只有第一个字母是可见的,而且它是巨大的).我需要它来适应TextView边界内的所有文本行.
这是我到目前为止:
public static void autoScaleTextViewTextToHeight(TextView tv)
{
final float initSize = tv.getTextSize();
//get the width of the view's back image (unscaled)....
float minViewHeight;
if(tv.getBackground()!=null)
{
minViewHeight = tv.getBackground().getIntrinsicHeight();
}
else
{
minViewHeight = 10f;//some min.
}
final float maxViewHeight = tv.getHeight() - (tv.getPaddingBottom()+tv.getPaddingTop())-12;// -12 just to be sure
final String s = tv.getText().toString();
//System.out.println(""+tv.getPaddingTop()+"/"+tv.getPaddingBottom());
if(minViewHeight >0 && maxViewHeight >2)
{
Rect currentBounds = new Rect();
tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
//System.out.println(""+initSize);
//System.out.println(""+maxViewHeight);
//System.out.println(""+(currentBounds.height())); …Run Code Online (Sandbox Code Playgroud)