Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเปลี่ยนสีตัวอักษร TextView ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการเปลี่ยนสีข้อความของ TextView ใน Android ทั้งผ่าน XML และโค้ด Java โดยใช้คลาส Color และเมธอด parseColor

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

ใน Android Studio ไปที่ File → New Project เลือก Empty View Activity แล้วตั้งชื่อโปรเจกต์

ขั้นตอนที่ 2: ออกแบบ Layout (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:text="สีของฉันคือสีน้ำเงิน"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="36sp"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_marginBottom="10dp"
        android:text="สีของฉันคือสีเขียว"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:textSize="36sp" />

</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity (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);
        // ใช้ค่าสีที่กำหนดไว้ในคลาส Color
        textView1.setTextColor(Color.BLUE);
        
        TextView textView2 = findViewById(R.id.textView2);
        // ใช้รหัสสีแบบ Hexadecimal
        textView2.setTextColor(Color.parseColor("#006400"));
    }
}

ขั้นตอนที่ 4: 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>

วิธีการอื่นๆ ในการกำหนดสี

  • Color.rgb(255, 0, 0) - กำหนดสีด้วยค่า RGB
  • Color.argb(255, 255, 0, 0) - กำหนดสีพร้อมค่า Alpha (ความโปร่งใส)
  • ContextCompat.getColor(context, R.color.your_color) - ดึงสีจาก resources (แนะนำสำหรับ API 23+)

การทดสอบ

รันแอปบนอุปกรณ์จริง จะเห็น TextView แรกเป็นสีน้ำเงิน และ TextView ที่สองเป็นสีเขียวเข้ม

วิธีเปลี่ยนสีตัวอักษร TextView ใน Android