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

Cách thay đổi cỡ chữ TextView trong Android

Giới thiệu

Bài viết này hướng dẫn cách thiết lập cỡ chữ (font size) cho TextView trong Android thông qua thuộc tính android:textSize trong XML. Đơn vị khuyến nghị là sp (scale-independent pixels) để hỗ trợ tùy chọn cỡ chữ của người dùng trong cài đặt hệ thống.

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

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và tạo dự án.

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

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Tôi to lắm"
        android:fontFamily="sans-serif-smallcaps"
        android:textSize="64sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp"
        android:text="Tôi nhỏ"
        android:fontFamily="serif-monospace"
        android:textSize="36sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp"
        android:text="Tôi siêu nhỏ"
        android:fontFamily="serif-monospace"
        android:textSize="24sp" />

</RelativeLayout>

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

import android.support.v7.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 AndroidManifest.xml

<?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. Giao diện hiển thị 3 TextView với cỡ chữ 64sp, 36sp, 24sp tương ứng, kèm theo font-family khác nhau.

Kiến thức bổ sung

  • Đơn vị sp vs dp: sp (scale-independent pixel) tự động.scale theo cỡ chữ hệ thống; dp (density-independent pixel) dùng cho kích thước layout.
  • Thay đổi cỡ chữ bằng code: textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 48)
  • Auto-size TextView (API 26+): Sử dụng app:autoSizeTextType="uniform" để tự động co giãn cỡ chữ vừa khung chứa.