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

วิธีตั้งค่าสีข้อความบางส่วนใน TextView ด้วย SpannableString ใน Kotlin

บทนำ

ตัวอย่างนี้สาธิตการใช้ SpannableString และ ForegroundColorSpan เพื่อเปลี่ยนสีข้อความเฉพาะส่วนใน TextView เดียว

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="SpannableString Demo"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold|italic" />
</RelativeLayout>

ขั้นตอนที่ 3: MainActivity.kt

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "SpannableString Demo"

        textView = findViewById(R.id.textView)
        
        val text = "I appreciate the prayer but I've already got God on my side"
        val spannableString = SpannableString(text)

        // สีชมพูสำหรับคำว่า "appreciate" (index 3-14)
        val spanMagenta = ForegroundColorSpan(Color.MAGENTA)
        spannableString.setSpan(spanMagenta, 3, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // สีแดงสำหรับ "prayer but" (index 22-30)
        val spanRed = ForegroundColorSpan(Color.RED)
        spannableString.setSpan(spanRed, 22, 31, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // สีเขียวสำห "already got" (index 38-48)
        val spanGreen = ForegroundColorSpan(Color.GREEN)
        spannableString.setSpan(spanGreen, 38, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        textView.text = spannableString
    }
}

ขั้นตอนที่ 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.spannable">
    <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>

Spannable Flags อธิบาย

Flagพฤติกรรม
SPAN_EXCLUSIVE_EXCLUSIVEไม่ขยายเมื่อเพิ่มข้อความข้างหน้า/ข้างหลัง (default)
SPAN_INCLUSIVE_INCLUSIVEขยายเมื่อเพิ่มข้อความทั้งสองข้าง
SPAN_EXCLUSIVE_INCLUSIVEขยายเฉพาะข้างหลัง
SPAN_INCLUSIVE_EXCLUSIVEขยายเฉพาะข้างหน้า

Span อื่นๆ ที่น่าสนใจ

  • StyleSpan - Bold, Italic, BoldItalic
  • RelativeSizeSpan - ปรับขนาดตัวอักษรเป็นสัดส่วน
  • UnderlineSpan, StrikethroughSpan - ขีดเส้นใต้/ทับ
  • ClickableSpan - ทำให้ส่วนของข้อความกดได้
  • ImageSpan - แทรกรูปภาพในข้อความ
  • TypefaceSpan - เปลี่ยน Font

ตัวอย่างรวมหลาย Span

val spannable = SpannableString("Bold Red Text").apply {
    setSpan(StyleSpan(Typeface.BOLD), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    setSpan(ForegroundColorSpan(Color.RED), 5, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    setSpan(RelativeSizeSpan(1.5f), 0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}