如何缩放/调整文本大小以适合TextView?

Rya*_*anM 27 android resize autosize textview

我正在尝试创建一种方法来调整多行文本的大小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()));

      float resultingSize = 1;
      while(currentBounds.height() < maxViewHeight)
      {
        resultingSize ++;
        tv.setTextSize(resultingSize);

        tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
        //System.out.println(""+(currentBounds.height()+tv.getPaddingBottom()+tv.getPaddingTop()));
        //System.out.println("Resulting: "+resultingSize);
      }
      if(currentBounds.height()>=maxViewHeight)
      {
        //just to be sure, reduce the value
        tv.setTextSize(resultingSize-1);
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为问题在于使用tv.getPaint().getTextBounds(...).它总是为文本边界返回小数字...相对于tv.getWidth()tv.getHeight()值小...即使文本大小远大于文本边界的宽度或高度TextView.

13r*_*ac1 20

MavenCentral的AutofitTextView库很好地处理了这个问题.来自Github(1k +星)的源代码位于https://github.com/grantland/android-autofittextview

将以下内容添加到您的 app/build.gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'me.grantland:autofittextview:0.2.+'
}
Run Code Online (Sandbox Code Playgroud)

在代码中启用任何View扩展TextView:

AutofitHelper.create(textView);
Run Code Online (Sandbox Code Playgroud)

启用任何以XML格式扩展TextView的View:

<me.grantland.widget.AutofitLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        />
</me.grantland.widget.AutofitLayout>
Run Code Online (Sandbox Code Playgroud)

在代码或XML中使用内置的Widget:

<me.grantland.widget.AutofitTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    />
Run Code Online (Sandbox Code Playgroud)


Jav*_*tar 6

Android O以来的新功能:

https://developer.android.com/preview/features/autosizing-textview.html

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:autoSizeTextType="uniform"
  android:autoSizeMinTextSize="12sp"
  android:autoSizeMaxTextSize="100sp"
  android:autoSizeStepGranularity="2sp"
/>
Run Code Online (Sandbox Code Playgroud)

  • 或者如果您使用支持库: app:autoSizeTextType="uniform" app:autoSizeMinTextSize="12sp" app:autoSizeMaxTextSize="100sp" app:autoSizeStepGranularity="2sp" 使用: xmlns:app="http://schemas .android.com/apk/res-auto” (3认同)

小智 5

我已经玩了很长一段时间了,试图在各种 7 英寸平板电脑(kindle fire、Nexus7 以及中国一些低分辨率屏幕的廉价平板电脑)和设备上调整字体大小。

最终对我有用的方法如下。“32”是一个任意因子,基本上可以在 7 英寸平板电脑水平线上提供大约 70 多个字符,这是我正在寻找的字体大小。进行相应调整。

textView.setTextSize(getFontSize(activity));


public static int getFontSize (Activity activity) { 

    DisplayMetrics dMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics);

    // lets try to get them back a font size realtive to the pixel width of the screen
    final float WIDE = activity.getResources().getDisplayMetrics().widthPixels;
    int valueWide = (int)(WIDE / 32.0f / (dMetrics.scaledDensity));
    return valueWide;
}
Run Code Online (Sandbox Code Playgroud)


snc*_*tln 2

也许在进行文本测量之前尝试将setHoriztonallyScrolling()设置为 true,这样 textView 就不会尝试将文本布局在多行上