Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách tự động điều chỉnh kích thước chữ TextView vừa khung trên Android

Giới thiệu

Bài viết này hướng dẫn cách tự động thu nhỏ/phóng to văn bản trong TextView để vừa vặn trong khu vực hiển thị (bounds) trên Android. Chúng ta sẽ sử dụng thư viện AutofitTextView của Grantland.

Các bước thực hiện

Bước 1: Tạo dự án và thêm dependency

Tạo dự án mới trong Android Studio. Thêm các dependency sau vào file build.gradle (Module: app):

implementation 'com.android.support:design:28.0.0'
implementation 'me.grantland:autofittextview:0.2.1'

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:autofit="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Android AutoFit TextView Example"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:singleLine="true"
        android:text="Một TextView tự động điều chỉnh cỡ chữ để vừa khung hiển thị"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Android AutoFit TextView"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:singleLine="true"
        android:text="Tự động thay đổi cỡ chữ"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Tự động điều chỉnh văn bản vừa hoàn hảo trong bounds"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />
</LinearLayout>

Bước 3: Code Activity (src/MainActivity.java)

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Bước 4: Cấu hình Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run và quan sát các AutofitTextView tự động thu nhỏ chữ để vừa khung.

Cách tự động điều chỉnh kích thước chữ TextView vừa khung trên Android

Lưu ý quan trọng

  • Thuộc tính autofit:minTextSize đặt cỡ chữ nhỏ nhất cho phép (đơn vị sp)
  • android:maxLines và android:singleLine kiểm soát số dòng hiển thị
  • Từ Android 8.0 (API 26), Android hỗ trợ sẵn TextView.setAutoSizeTextTypeWithDefaults() không cần thư viện bên thứ ba