[Android] Detailed explanation of the use of AppCompatTextView

The role of AppCompatTextView

The function of AppCompatTextView is to automatically adjust the font size according to the number of words to prevent the text from being truncated or ellipsis appearing when there are too many words. However, the traditional TextView cannot automatically adjust the text size, and it will be truncated or display an ellipsis if it exceeds the text size.

For example, there is a TextView whose maximum width is 100dp, but its text length is uncertain, and it is usually sent by the server. At this time, the product needs to be fully displayed regardless of the number of characters. When there are many characters, it will automatically become smaller, and when there are few characters, it will automatically become larger. No truncation or ellipsis is allowed.

In this case, TextView does not meet our needs, and AppCompatTextView makes its debut.

Example of using AppCompatTextView

Here is an example of usage:

<?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"
    android:padding="10dp">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="A测试字号大小测试字号大小测试字号大小测试字号大小测试字号大小测试字号大小B"
        app:autoSizeTextType="uniform" 
        app:autoSizeMinTextSize="10sp"
        app:autoSizeMaxTextSize="36sp"
        app:autoSizeStepGranularity="1sp"
        />
</LinearLayout>

When the number of characters is relatively small, the display effect is as follows:
insert image description here
when the number of characters is large, the display effect is as follows:
insert image description here

Steps to use AppCompatTextView

The complete usage steps are as follows:

(1) Replace TextView with AppCompatTextView.

(2) Add attribute: android:maxLines="1"Or android:lines="1", set it to single-line display.
Be careful not to use it android:singleLine="true", otherwise the scaling will be invalid.

(3), add attribute: app:autoSizeTextType="uniform"set to automatic scaling of the font size.
This attribute has only two values: uniform and none. The official explanation is as follows:

    <!-- Specify the type of auto-size. Note that this feature is not supported by EditText,
        works only for TextView. -->
        <attr format="enum" name="autoSizeTextType">
            <!-- No auto-sizing (default). -->
            <enum name="none" value="0"/>
            <!-- Uniform horizontal and vertical text size scaling to fit within the
            container. -->
            <enum name="uniform" value="1"/>
        </attr>

(4) Add attribute: app:autoSizeStepGranularity="1sp"set the minimum zoom step to 1sp, and default to 1px if not specified.

(5) Set the minimum font size: app:autoSizeMinTextSize="8sp", set according to the actual situation.

(6). Set the maximum font size: app:autoSizeMaxTextSize="36sp", set according to the actual situation.

After completing the above steps, the font size can be automatically scaled (adapted).

Precautions for using AppCompatTextView

(1), android:layout_widthset to a fixed value, wrap_content, match_parent are all possible.

(2) It is also possible to use AppCompatTextView in combination with RecyclerView.

(3) When dynamically setting text to AppCompatTextView in the Adapter of RecyclerView, do not use SpannableString, otherwise the scaling will be invalid. If you must use SpannableString, you can convert it to String (call toString()), but the function of SpannableString will be invalid.

Guess you like

Origin blog.csdn.net/devnn/article/details/121274795