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

Cách thay đổi màu chữ TextView trong Android

Giới thiệu

Bài viết này trình bày 2 cách phổ biến để thay đổi màu chữ của TextView trong Android: sử dụng hằng số màu có sẵn (Color.BLUE) và mã màu hex (Color.parseColor()).

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="Màu của tôi là Xanh dương"
        android:textSize="36sp"
        android:textStyle="bold" />

    <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="Màu của tôi là Xanh lá"
        android:textSize="36sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: Viết code logic (src/MainActivity.java)

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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

        TextView textView1 = findViewById(R.id.textView1);
        textView1.setTextColor(Color.BLUE); // Sử dụng hằng số màu

        TextView textView2 = findViewById(R.id.textView2);
        textView2.setTextColor(Color.parseColor("#006400")); // Sử dụng mã hex
    }
}

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 và quan sát: TextView đầu tiên hiển thị màu xanh dương, TextView thứ hai hiển thị màu xanh lá đậm (#006400).

Các cách khác để đặt màu chữ

  • XML: android:textColor="#FF0000" hoặc android:textColor="@color/ten_mau"
  • ContextCompat: ContextCompat.getColor(context, R.color.ten_mau) (khuyên dùng cho API 23+)
  • ColorStateList: Để thay đổi màu theo trạng thái (pressed, focused, disabled...)