~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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)