เพิ่มหน้าจอแนะนํา

หากแอปของคุณใช้หน้าจอเริ่มต้นที่กำหนดเองหรือใช้ธีม Launcher ให้ย้าย แอปไปยังไลบรารี SplashScreen ซึ่งมีให้บริการใน Jetpack เพื่อให้แน่ใจว่าแอป แสดงอย่างถูกต้องใน Wear OS ทุกเวอร์ชัน

ดูวิธีการใช้งานทีละขั้นตอนในหน้านี้เพื่อเรียนรู้วิธีเพิ่มหน้าจอเริ่มต้นโดยใช้ไลบรารี SplashScreen เพื่อให้หน้าจอเป็นไปตามหลักเกณฑ์การออกแบบ

เพิ่มทรัพยากร Dependency

เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในไฟล์ build.gradle ของโมดูลแอป

Groovy

dependencies {
    implementation "androidx.core:core-splashscreen:1.2.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-splashscreen:1.2.0")
}

ตรวจสอบว่าคุณใช้เวอร์ชัน 1.0.1 ขึ้นไปเพื่อรับการสนับสนุนขนาดเริ่มต้นของ Wear OS

เพิ่มธีม

สร้างธีมหน้าจอเริ่มต้นใน res/values/styles.xml องค์ประกอบระดับบนจะขึ้นอยู่กับรูปร่างของไอคอน

  • หากไอคอนเป็นวงกลม ให้ใช้ Theme.SplashScreen
  • หากไอคอนมีรูปร่างอื่น ให้ใช้ Theme.SplashScreen.IconBackground

ใช้ windowSplashScreenBackground เพื่อเติมพื้นหลังด้วยสีดำสีเดียว ตั้งค่า postSplashScreenTheme เป็นธีมที่กิจกรรมควรใช้ และ windowSplashScreenAnimatedIcon เป็น Drawable หรือ Animated Drawable

<resources>
    <style name="Theme.App" parent="@android:style/Theme.DeviceDefault" />

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <!-- Set the splash screen background to black -->
        <item name="windowSplashScreenBackground">@android:color/black</item>
        <!-- Use windowSplashScreenAnimatedIcon to add a drawable or an animated
             drawable. -->
        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen</item>
        <!-- Set the theme of the Activity that follows your splash screen. -->
        <item name="postSplashScreenTheme">@style/Theme.App</item>
    </style>
</resources>

หากใช้ไอคอนที่ไม่ใช่รูปวงกลม คุณต้องตั้งค่าสีพื้นหลังเป็นสีขาวใต้ไอคอน ในกรณีนี้ ให้ใช้ Theme.SplashScreen.IconBackground เป็นธีมระดับบนและตั้งค่าแอตทริบิวต์ windowSplashScreenIconBackgroundColor

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
    <!-- Set a white background behind the splash screen icon. -->
    <item name="windowSplashScreenIconBackgroundColor">@android:color/white</item>
</style>

แอตทริบิวต์อื่นๆ เป็นตัวเลือก

สร้าง Drawable สำหรับธีม

ธีมหน้าจอเริ่มต้นต้องมี Drawable เพื่อส่งไปยังแอตทริบิวต์ windowSplashScreenAnimatedIcon ตัวอย่างเช่น คุณสร้างได้โดยเพิ่มไฟล์ใหม่ res/drawable/splash_screen.xml และใช้ไอคอน Launcher ของแอปและขนาดไอคอนหน้าจอเริ่มต้นที่ถูกต้อง

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="@dimen/splash_screen_icon_size"
        android:height="@dimen/splash_screen_icon_size"
        android:drawable="@mipmap/ic_launcher"
        android:gravity="center" />
</layer-list>

ขนาดไอคอนหน้าจอเริ่มต้นกำหนดไว้ใน res/values/dimens.xml และจะแตกต่างกันไปตามว่าไอคอนเป็นรูปวงกลมหรือไม่

<resources>
    <!-- Round app icon can take all of default space -->
    <dimen name="splash_screen_icon_size">48dp</dimen>
</resources>

...หรือไม่ใช่รูปวงกลม จึงต้องใช้พื้นหลังไอคอน

<resources>
    <!-- Non-round icon with background must use reduced size to fit circle -->
    <dimen name="splash_screen_icon_size">36dp</dimen>
</resources>

ระบุธีม

ในไฟล์ Manifest ของแอป (AndroidManifest.xml) ให้แทนที่ธีมของกิจกรรมเริ่มต้น ซึ่งโดยปกติจะเป็นกิจกรรมที่กำหนดรายการ Launcher หรือส่งออกอื่นๆ ด้วยธีมที่คุณสร้างขึ้นในขั้นตอนก่อนหน้า

<activity
    android:name=".snippets.SplashScreenActivity"
    android:exported="true"
    android:taskAffinity=""
    android:theme="@style/Theme.App.Starting">
    <!-- ... -->
</activity>

อัปเดตกิจกรรมเริ่มต้น

ติดตั้งหน้าจอเริ่มต้นในกิจกรรมเริ่มต้นก่อนเรียกใช้ super.onCreate() และโหลด Composables

class SplashScreenActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)

        setContent {
            WearApp()
        }
    }
}

แหล่งข้อมูลเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับหน้าจอเริ่มต้นโดยทั่วไปและวิธีใช้หน้าจอเริ่มต้น ในแอป