การเปลี่ยนสีพื้นหลัง (Background Color) ของ View ใน Android
ตัวอย่างนี้แสดงวิธีตั้งค่าสีพื้นหลังให้ TextView (หรือ View ใดๆ) ผ่าน XML Layout โดยใช้ค่า Hex Color
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด → Finish
ขั้นตอนที่ 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:background="#046B5F"
android:padding="16dp"
android:text="สีพื้นหลังของ View นี้"
android:textColor="#FFFFFF" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java
package app.com.sample;
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);
}
}
ขั้นตอนที่ 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>
วิธีตั้งค่าสีพื้นหลังต่างๆ
1. ผ่าน XML (Static)
<View
android:background="#FF0000" <!-- Red -->
android:background="#046B5F" <!-- Teal -->
android:background="@color/colorPrimary" <!-- จาก colors.xml -->
android:background="@android:color/holo_blue_light" <!-- System color --> />
2. ผ่านโค้ด Java/Kotlin
// ใช้ Color.parseColor (Hex String)
view.setBackgroundColor(Color.parseColor("#046B5F"));
// ใช้ Color RGB
view.setBackgroundColor(Color.rgb(4, 107, 95));
// ใช้ Color ARGB (พร้อม Alpha)
view.setBackgroundColor(Color.argb(128, 4, 107, 95)); // 50% transparent
// ใช้ Resource Color (API 23+)
view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
// ใช้ Resource Color (Deprecated API <23)
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
3. ใช้ Drawable Resource
<!-- res/drawable/bg_custom.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#046B5F" />
<corners android:radius="8dp" />
<stroke android:width="2dp" android:color="#FFFFFF" />
</shape>
<!-- ใช้ใน Layout -->
android:background="@drawable/bg_custom"
4. ใช้ Gradient
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:startColor="#046B5F"
android:endColor="#1DE9B6"
android:angle="45" />
</shape>
รูปแบบสี Hex
| รูปแบบ | ตัวอย่าง | คำอธิบาย |
|---|---|---|
| #RGB | #F00 | 3 หลัก (Shorthand) |
| #ARGB | #8F00 | 4 หลัก พร้อม Alpha |
| #RRGGBB | #FF0000 | 6 หลัก มาตรฐาน |
| #AARRGGBB | #80FF0000 | 8 หลัก พร้อม Alpha (API 26+) |
การทดสอบ
รันแอป → จะเห็น TextView พื้นหลังสีเขียวเข้ม (#046B5F) ตัวอักษรสีขาว อยู่ตรงกลางหน้าจอ