~Onlooker 2011-02-17 18:31 采纳率: 0%
浏览 899
已采纳

自动缩放 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

  • 写回答

27条回答 默认 最新

  • 叼花硬汉 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!

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器