Android谷歌官方的自适应TextView字体大小的解决方案

Android谷歌官方的自适应TextView字体大小的解决方案

有时候UI限定了TextView宽度或者布局的控件空间尺寸不足,而此时的TextView里面的文本就无法完全正常显示。自适应TextView文本大小字体以适应限定宽度的TextView有不少方法,现在给出一种Android官方的自适应字体方法,直接在xml即可自适应。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:maxLines="1"
        android:text="zhang phil @ csdn"
        android:textColor="@android:color/white"
        android:textSize="80dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:autoSizeMaxTextSize="80dp"
        android:autoSizeMinTextSize="2dp"
        android:autoSizeTextType="uniform"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:maxLines="1"
        android:text="zhang phil @ csdn"
        android:textColor="@android:color/white"
        android:textSize="80dp" />
</LinearLayout>


输出结果:


目标是完整显示“zhang phil @ csdn”字符串,两个TextView都特意把字体大小和TextView的宽度均设为80dp(故意制造无法完整显示完全的情景)。作为对比,蓝色的TextView没有做自适应处理,导致“zhang phil @ csdn”只能显示一个“z”,而红色的做自适应处理,能自适应的缩小字体完整的显示全部文本“zhang phil @ csdn”。

关键代码:

        android:autoSizeMaxTextSize="80dp"
        android:autoSizeMinTextSize="2dp"
        android:autoSizeTextType="uniform"

注意要把属性maxLines设置为1,否则可能会换行。


附录:
1,《Android TextView利用measureText自适应文本字体大小宽度》链接:https://blog.csdn.net/zhangphil/article/details/79891765

您好!要实现安卓TextView自适应字体大小的效果,可以使用以下方法: 1. 使用AutoSizeTextType属性:在XML布局文件中,将TextView的属性设置为`app:autoSizeTextType="uniform"`,这将使TextView字体大小根据其宽度和高度自动调整。 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:autoSizeTextType="uniform" /> ``` 2. 使用AutoSizeTextView库:如果您的项目支持Android 8.0(API级别26)以下的版本,可以使用Google提供的AutoSizeTextView库。首先,在项目的build.gradle文件中添加以下依赖项: ```groovy dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:preference-v7:28.0.0' implementation 'com.android.support:autosize:1.1.2' } ``` 接下来,在XML布局文件中使用`AutoSizeTextView`代替`TextView`: ```xml <com.google.android.material.textview.AutoSizeTextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 这样,AutoSizeTextView会自动根据TextView的宽度和高度调整字体大小。 3. 使用代码适配字体大小:您还可以通过编写代码来适应TextView字体大小。可以使用Paint类中的`setTextSize()`方法来设置字体大小,并根据TextView的宽度和高度进行调整。 ```java TextView textView = findViewById(R.id.textView); textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeOnGlobalLayoutListener(this); int width = textView.getWidth(); int height = textView.getHeight(); float fontSize = calculateFontSize(width, height); // 根据宽度和高度计算字体大小 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize); } }); private float calculateFontSize(int width, int height) { // 根据宽度和高度计算字体大小的逻辑 // 可以根据需求自定义计算方法 } ``` 以上是几种实现安卓TextView自适应字体大小的方法,您可以根据项目需求选择适合您的方法进行使用。希望对您有所帮助!如果还有其他问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangphil

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

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

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

打赏作者

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

抵扣说明:

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

余额充值