Android新增AppCompatTextView自适应字体大小和文本宽度


Android新增AppCompatTextView自适应字体大小和文本宽度

Android的supportv7包中新增了一个AppCompatTextView,它是Android标准TextView的增强:

package android.support.v7.widget;

public class AppCompatTextView extends TextView implements TintableBackgroundView,
        AutoSizeableTextView {

}

AppCompatTextView最显著的特点是可以自适应字体宽度大小变化。这个特点很有用,有些开发场景下,UI设计限定了一个文本的宽度,但是文本的长度可能比较长,如果设定一个固定的textSize,就导致一部分文本无法显示。而AppCompatTextView就是为此而生,设定三个参数就可以让文本随着文本宽度的变化,限定在一个固定范围内完整显示出来:

<android.support.v7.widget.AppCompatTextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:autoSizeMinTextSize="8dp"
    android:autoSizeTextType="uniform"
    android:autoSizeMaxTextSize="18dp"
    android:maxLines="1"
    android:text="-.-"
    android:textSize="18dp" />

在布局里面把过去的TextView换成AppCompatTextView,然后配置:
android:autoSizeTextType="uniform"
即可自适应宽度。
android:autoSizeMinTextSize="8dp",约束虽然可以自适应调整字体大小,但最小不能小于8dp。
android:autoSizeMaxTextSize="18dp",限定字体大小自适应调整变化时候,最大不能超过的数值。


需要注意的是,根据Android官方对AppCompatTextView的解释说:
/**
 * A {@link TextView} which supports compatible features on older versions of the platform,
 * including:
 * <ul>
 *     <li>Allows dynamic tint of its background via the background tint methods in
 *     {@link android.support.v4.view.ViewCompat}.</li>
 *     <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and
 *     {@link R.attr#backgroundTintMode}.</li>
 *     <li>Supports auto-sizing via {@link android.support.v4.widget.TextViewCompat} by allowing
 *     to instruct a {@link TextView} to let the size of the text expand or contract automatically
 *     to fill its layout based on the TextView's characteristics and boundaries. The
 *     style attributes associated with auto-sizing are {@link R.attr#autoSizeTextType},
 *     {@link R.attr#autoSizeMinTextSize}, {@link R.attr#autoSizeMaxTextSize},
 *     {@link R.attr#autoSizeStepGranularity} and {@link R.attr#autoSizePresetSizes}, all of
 *     which work back to
 *     {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH Ice Cream Sandwich}.</li>
 * </ul>
 *
 * <p>This will automatically be used when you use {@link TextView} in your layouts
 * and the top-level activity / dialog is provided by
 * <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
 * You should only need to manually use this class when writing custom views.</p>
 */


这段文档中最后一段比较重要,Android官方提示开发者,如果开发者在xml布局中写了一个过去传统的Android的TextView,那么这个TextView会被自动被Android编译系统替换成AppCompatTextView。在在Android O(8.0)系统及以上,TextView和AppCompatTextView是相同的。在低于8.0的版本上,开发者可在自定义和布局中需要写文本View时候,可以使用AppCompatTextView,以使用到Android最新的自适应大小的特性。

在过去,没有AppCompatTextView官方这一自适应字体大小text view时候,开发者常常使用github上的一个开源项目:

me.grantland.widget.AutofitTextView

然而,AutofitTextView也很久没有维护了。何况现在有了Android官方提供的支持,建议开发者以后可以使用AppCompatTextView用以支持字体的自适应大小变化。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值