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

วิธีใช้ Theme เฉพาะ Activity ใน Android

การใช้ Theme แบบเฉพาะ Activity (Per-Activity Theme) ใน AndroidManifest

ตัวอย่างนี้แสดงวิธีกำหนด Theme ให้ Activity เฉพาะตัว โดยใช้ attribute android:theme ในแท็ก <activity> แทนที่จะกำหนดที่ Application Level

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

Android Studio → File → New Project → กรอกรายละเอียด → Finish

ขั้นตอนที่ 2: Layout - res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    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:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 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 - ใช้ Theme เฉพาะ Activity

<?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"> <!-- Default Theme สำหรับทั้งแอป -->
        
        <activity 
            android:name=".MainActivity" 
            android:theme="@style/Theme.AppCompat.Dialog.MinWidth"> <!-- Theme เฉพาะ Activity นี้ -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Theme สำเร็จรูปที่นิยมใช้ต่อ Activity

สร้าง Custom Theme สำหรับ Activity เฉพาะ

<!-- res/values/styles.xml -->
<style name="CustomActivityTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/custom_primary</item>
    <item name="colorPrimaryDark">@color/custom_primary_dark</item>
    <item name="colorAccent">@color/custom_accent</item>
    <item name="android:windowBackground">@color/custom_bg</item>
    <item name="android:windowIsTranslucent">true</item> <!-- โปร่งใส -->
    <item name="android:windowAnimationStyle">@style/CustomAnimation</item>
</style>

ใช้ใน Manifest:

<activity android:name=".SpecialActivity" android:theme="@style/CustomActivityTheme" />

เปลี่ยน Theme ด้วยโค้ด (Runtime)

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ต้องเรียกก่อน super.onCreate() และ setContentView()
    setTheme(R.style.CustomActivityTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

กรณีการใช้งานทั่วไป

  • Splash Screen: Theme โปร่งใส/ไม่มี Action Bar
  • Settings/Dialog Activity: Theme.Dialog
  • Full Screen Media: Theme.NoActionBar + Fullscreen Flags
  • Print/Export: Theme เหมาะสำหรับพิมพ์
  • Dark Mode Test: บังคับ Theme.DayNight เพื่อทดสอบ

⚠️ ข้อควรระวัง

  • Theme ต้องตั้งค่าก่อน super.onCreate() และ setContentView()
  • การเปลี่ยน Theme รันไทม์จะทำให้ Activity Recreate
  • Manifest Theme มี Priority สูงกว่า setTheme() ในโค้ด
  • Theme.Dialog ทำให้ Activity ไม่ได้เป็น Full Screen

การทดสอบ

รันแอป → MainActivity จะแสดงเป็น Dialog กว้างเต็มจอ (MinWidth) แทนที่จะเป็น Full Screen Activity ปกติ

Themeลักษณะใช้เมื่อไหร่
Theme.AppCompat.DialogDialog ธีม มุมโค้ง มีเงาActivity แบบ Dialog/Modal
Theme.AppCompat.Dialog.MinWidthDialog กว้างเต็มจอ (Min Width)Bottom Sheet, Full-width Dialog
Theme.AppCompat.Dialog.AlertAlert Dialog ธีมConfirmation Dialog
Theme.AppCompat.Light.NoActionBarLight Theme ไม่มี Action BarFull Screen Activity
Theme.AppCompat.DayNight.NoActionBarรองรับ Dark Modeแอปที่มี Dark/Light Theme
Theme.MaterialComponents.Light.DialogMaterial Design DialogMaterial Design 3
Theme.Translucent.NoTitleBarโปร่งใส ไม่มี TitleOverlay, Splash Screen