自动缩放 TextView 文本以适应边界 [英] Auto Scale TextView Text to Fit within Bounds

查看:32
本文介绍了自动缩放 TextView 文本以适应边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种最佳方式来调整 TextView 中的换行文本的大小,使其适合其 getHeight 和 getWidth 边界.我不仅仅是在寻找一种包装文本的方法 - 我想确保它既可以包装又足够小以完全适合屏幕.

我在 StackOverflow 上看到过一些需要自动调整大小的情况,但它们要么是具有 hack 解决方案的非常特殊的情况,要么没有解决方案,要么涉及递归地重新绘制 TextView 直到它足够小(这会占用大量内存并迫使用户在每次递归时逐步缩小文本).

但我相信有人已经找到了一个很好的解决方案,它不涉及我正在做的事情:编写几个繁重的例程来解析和测量文本,调整文本大小,然后重复直到合适的小尺寸被发现.

TextView 使用哪些例程来包装文本?难道这些不能以某种方式用于预测文本是否足够小?

tl;dr:是否有一种最佳实践方法可以自动调整 TextView 的大小以适应、包裹在其 getHeight 和 getWidth 范围内?

解决方案

从 2018 年 6 月起,Android 正式开始为 Android 4.0(API 级别 14)及更高版本支持此功能.
查看:Autosizing TextViews

使用 Android 8.0(API 级别 26)及更高版本:

以编程方式:

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


Android 8.0(API 级别 26)之前的 Android 版本:

<文本视图android:layout_width="match_parent"android:layout_height="200dp"应用程序:autoSizeTextType =统一"应用程序:autoSizeMinTextSize="12sp"应用程序:autoSizeMaxTextSize="100sp"app:autoSizeStepGranularity="2sp"/></LinearLayout>

以编程方式:

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

注意:TextView 必须有 layout_width="ma​​tch_parent" 或 绝对大小!

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?

解决方案

From June 2018 Android officially started supporting 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" />

Programmatically:

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>

Programmatically:

TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
TextView textView, 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!

这篇关于自动缩放 TextView 文本以适应边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆