หากแอปของคุณใช้หน้าจอเริ่มต้นที่กำหนดเองหรือใช้ธีม 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 และใช้ไอคอนตัวเปิดแอป
และขนาดไอคอนหน้าจอเริ่มต้นที่ถูกต้อง
<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) ให้แทนที่ธีมของ
กิจกรรมเริ่มต้น ซึ่งมักจะเป็นกิจกรรมที่กำหนดรายการตัวเรียกใช้หรือ
ส่งออกอื่นๆ ด้วยธีมที่คุณสร้างในขั้นตอนก่อนหน้า
<activity android:name=".snippets.SplashScreenActivity" android:exported="true" android:taskAffinity="" android:theme="@style/Theme.App.Starting"> <!-- ... --> </activity>
อัปเดตกิจกรรมเริ่มต้น
ติดตั้งหน้าจอเริ่มต้นในกิจกรรมเริ่มต้นก่อนเรียกใช้
super.onCreate()
class SplashScreenActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) setContent { WearApp() } } }
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับหน้าจอแนะนำโดยทั่วไปและวิธีใช้ในแอป
แนะนำสำหรับคุณ
- หมายเหตุ: ข้อความลิงก์จะแสดงเมื่อ JavaScript ปิดอยู่
- ย้ายข้อมูลการติดตั้งใช้งานหน้าจอแนะนําไปยัง Android 12 ขึ้นไป
- หน้าจอเริ่มต้น
- ผสานรวม App Actions กับวิดเจ็ต Android