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

วิธีตั้งค่าสีพื้นหลัง Activity เป็นสีเหลืองโดยโปรแกรม

ภาพรวม

ตัวอย่างนี้สาธิตการเปลี่ยนสีพื้นหลังของ Activity (Root View) เป็นสีเหลืองผ่านโค้ด Java

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

เปิด Android Studio → File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: เลย์เอาต์ (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"
    android:id="@+id/relativeLayout"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="24sp"
        android:text="The color of the Activity is Yellow" />

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

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

public class MainActivity extends AppCompatActivity {
    RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        relativeLayout = findViewById(R.id.relativeLayout);
        
        // วิธี 1: ใช้ Color class สีสำเร็จรูป
        relativeLayout.setBackgroundColor(Color.YELLOW);
        
        // วิธี 2: ใช้ Color.parseColor (Hex)
        // relativeLayout.setBackgroundColor(Color.parseColor("#FFFF00"));
        
        // วิธี 3: ใช้ RGB
        // relativeLayout.setBackgroundColor(Color.rgb(255, 255, 0));
        
        // วิธี 4: ใช้ ARGB (มี Alpha)
        // relativeLayout.setBackgroundColor(Color.argb(255, 255, 255, 0));
        
        // วิธี 5: ใช้ Resource Color (API 23+)
        // relativeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
    }
}

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

การทดสอบ

รันแอปพลิเคชัน จะเห็นพื้นหลังทั้งหน้าจอเป็นสีเหลือง

วิธีตั้งค่าสีพื้นหลัง Activity เป็นสีเหลืองโดยโปรแกรม

วิธีการตั้งค่าสีพื้นหลังแบบต่างๆ

1. ตั้งค่าใน XML (แนะนำสำหรับสีคงที่)

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow" />

<!-- res/values/colors.xml -->
<color name="yellow">#FFFF00</color>

2. ใช้ Theme/Style (สำหรับทั้งแอป)

<!-- res/values/styles.xml -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/yellow</item>
</style>

3. ใช้ Drawable (Gradient, Shape)

<!-- res/drawable/bg_gradient.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#FFFF00"
        android:endColor="#FFA500"
        android:angle="45" />
</shape>

<!-- ใช้ในโค้ด -->
relativeLayout.setBackgroundResource(R.drawable.bg_gradient);

4. สำหรับ Activity ทั้งหน้า (Window Background)

// ใน onCreate() ก่อน setContentView()
getWindow().setBackgroundDrawableResource(R.drawable.bg_gradient);
// หรือ
getWindow().getDecorView().setBackgroundColor(Color.YELLOW);
💡 Best Practice: ควรตั้งค่าสีใน XML/Theme แทนโค้ด เพราะ:
  • แยก UI Logic ออกจาก Business Logic
  • ง่ายต่อการ Maintenance และ Theming
  • รองรับ Dark Mode ได้ดีกว่า
  • Performance ดีกว่า (ไม่ต้อง FindViewById + Set)