狐狸.fox 2011-02-17 18:31 采纳率: 0%
浏览 612
已采纳

自动缩放 TextView 文本以适应边界

I'm looking for an optimal way to resize wrapping text in a TextView so that it will fit within its getHeight and getWidth bounds. I'm not simply looking for a way to wrap the text- I want to make sure it both wraps and is small enough to fit entirely on the screen.

I've seen a few cases on StackOverflow where auto resizing was needed, but they are either very special cases with hack solutions, have no solution, or involve re-drawing the TextView recursively until it is small enough (which is memory intense and forces the user to watch the text shrink step-by-step with every recursion).

But I'm sure somebody out there has found a good solution that doesn't involve what I'm doing: writing several heavy routines that parse and measure the text, resize the text, and repeat until a suitably small size has been found.

What routines does TextView use to wrap the text? Couldn't those be somehow used to predict whether text will be small enough?

tl;dr: is there a best-practice way to auto-resize a TextView to fit, wrapped, in its getHeight and getWidth bounds?

转载于:https://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds

  • 写回答

30条回答 默认 最新

  • 10.24 2018-10-12 05:06
    关注

    Thank god!, from June 2018 Android officially support this feature for Android 4.0 (API level 14) and higher.
    Check it out at: Autosizing TextViews

    With Android 8.0 (API level 26) and higher:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:autoSizeTextType="uniform"
        android:autoSizeMinTextSize="12sp"
        android:autoSizeMaxTextSize="100sp"
        android:autoSizeStepGranularity="2sp" />
    

    Programatically:

    setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, 
            int autoSizeStepGranularity, int unit)
    
    textView.setAutoSizeTextTypeUniformWithConfiguration(
                    1, 17, 1, TypedValue.COMPLEX_UNIT_DIP);
    


    Android versions prior to Android 8.0 (API level 26):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
      <TextView
          android:layout_width="match_parent"
          android:layout_height="200dp"
          app:autoSizeTextType="uniform"
          app:autoSizeMinTextSize="12sp"
          app:autoSizeMaxTextSize="100sp"
          app:autoSizeStepGranularity="2sp" />
    
    </LinearLayout>
    

    Programatically:

    TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
    int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) 
    
    TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView, 1, 17, 1,
    TypedValue.COMPLEX_UNIT_DIP);
    

    Attention: TextView must have layout_width="match_parent" or absolute size!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能