บทเรียนนี้จะอธิบายวิธีสร้างการใช้งานที่มิเรอร์ API เวอร์ชันใหม่แต่รองรับอุปกรณ์รุ่นเก่า
ตัดสินใจเลือกโซลูชันอื่น
งานที่มีความท้าทายที่สุดในการใช้ฟีเจอร์ UI เวอร์ชันใหม่ในลักษณะที่ใช้งานย้อนหลังได้คือการเลือกและติดตั้งใช้งานโซลูชันเวอร์ชันเก่า (สำรอง) สำหรับแพลตฟอร์มเวอร์ชันเก่า ในหลายกรณี คุณสามารถทำให้คอมโพเนนต์ UI ใหม่ๆ เหล่านี้ใช้งานได้ตรงตามวัตถุประสงค์ของคอมโพเนนต์ UI ใหม่เหล่านี้โดยใช้ฟีเจอร์เฟรมเวิร์ก UI แบบเก่า เช่น
-
คุณสามารถใช้แถบการดำเนินการโดยใช้
LinearLayout
แนวนอนที่มีปุ่มรูปภาพ ไม่ว่าจะใช้เป็นแถบชื่อที่กำหนดเองหรือเป็นมุมมองในเลย์เอาต์กิจกรรม การดำเนินการรายการเพิ่มเติมจะแสดงใต้ปุ่มเมนูของอุปกรณ์ได้ -
คุณติดตั้งแท็บแถบการดำเนินการได้โดยใช้
LinearLayout
แนวนอนที่มีปุ่ม หรือใช้องค์ประกอบ UITabWidget
-
คุณใช้งานวิดเจ็ต
NumberPicker
และSwitch
ได้โดยใช้วิดเจ็ตSpinner
และToggleButton
ตามลำดับ -
คุณติดตั้งใช้งานวิดเจ็ต
ListPopupWindow
และPopupMenu
ได้โดยใช้วิดเจ็ตPopupWindow
โดยทั่วไปแล้ว ไม่มีโซลูชันใดใช้ได้ผลสำหรับการย้ายคอมโพเนนต์ UI ที่ใหม่กว่าไปยังอุปกรณ์รุ่นเก่า คำนึงถึงประสบการณ์ของผู้ใช้: ผู้ใช้อาจไม่คุ้นเคยกับรูปแบบการออกแบบและคอมโพเนนต์ UI ในอุปกรณ์รุ่นเก่า ลองพิจารณาว่าคุณจะนำเสนอฟังก์ชันการทำงานเดียวกันนี้โดยใช้องค์ประกอบที่คุ้นเคยได้อย่างไร ในหลายกรณี ปัญหานี้ไม่น่ากังวลนัก เช่น การที่คอมโพเนนต์ UI ใหม่มีความโดดเด่นในระบบนิเวศของแอปพลิเคชัน (เช่น แถบการทำงาน) หรือเมื่อรูปแบบการโต้ตอบนั้นง่ายและใช้งานง่ายมาก (เช่น การดูด้วยการปัดโดยใช้ ViewPager
)
ใช้แท็บโดยใช้ API เก่า
หากต้องการสร้างแท็บแถบการดำเนินการแบบเก่า คุณสามารถใช้ TabWidget
และ TabHost
(หรือจะใช้วิดเจ็ต Button
ที่วางแนวนอนแทนก็ได้) ติดตั้งใช้งานในคลาสที่ชื่อ TabHelperEclair
และ CompatTabEclair
เนื่องจากการติดตั้งใช้งานนี้ใช้ API ที่เปิดตัวใน Android 2.0 (Eclair) ขึ้นไป
![แผนภาพคลาสสําหรับการใช้งานแท็บของ Eclair](https://developer.android.google.cn/static/images/training/backward-compatible-ui-classes-eclair.png?hl=th)
รูปที่ 1 แผนภาพชั้นเรียนสำหรับการติดตั้งใช้งานแท็บของ Eclair
การใช้งาน CompatTabEclair
จะจัดเก็บพร็อพเพอร์ตี้แท็บ เช่น ข้อความและไอคอนของแท็บในตัวแปรอินสแตนซ์ เนื่องจากไม่มีออบเจ็กต์ ActionBar.Tab
ที่พร้อมจัดการพื้นที่เก็บข้อมูลนี้
Kotlin
class CompatTabEclair internal constructor(val activity: FragmentActivity, tag: String) : CompatTab(tag) { // Store these properties in the instance, // as there is no ActionBar.Tab object. private var text: CharSequence? = null ... override fun setText(resId: Int): CompatTab { // Our older implementation simply stores this // information in the object instance. text = activity.resources.getText(resId) return this } ... // Do the same for other properties (icon, callback, etc.) }
Java
public class CompatTabEclair extends CompatTab { // Store these properties in the instance, // as there is no ActionBar.Tab object. private CharSequence text; ... public CompatTab setText(int resId) { // Our older implementation simply stores this // information in the object instance. text = activity.getResources().getText(resId); return this; } ... // Do the same for other properties (icon, callback, etc.) }
การใช้งาน TabHelperEclair
จะใช้เมธอดในวิดเจ็ต TabHost
เพื่อสร้างออบเจ็กต์ TabHost.TabSpec
และตัวบอกแท็บ ดังนี้
Kotlin
class TabHelperEclair internal constructor(activity: FragmentActivity) : TabHelper(activity) { private var tabHost: TabHost? = null ... override fun setUp() { // Our activity layout for pre-Honeycomb devices // must contain a TabHost. tabHost = tabHost ?: mActivity.findViewById<TabHost>(android.R.id.tabhost).apply { setup() } } override fun addTab(tab: CompatTab) { ... tabHost?.newTabSpec(tab.tag)?.run { setIndicator(tab.getText()) // And optional icon ... tabHost?.addTab(this) } } // The other important method, newTab() is part of // the base implementation. }
Java
public class TabHelperEclair extends TabHelper { private TabHost tabHost; ... protected void setUp() { if (tabHost == null) { // Our activity layout for pre-Honeycomb devices // must contain a TabHost. tabHost = (TabHost) mActivity.findViewById( android.R.id.tabhost); tabHost.setup(); } } public void addTab(CompatTab tab) { ... TabSpec spec = tabHost .newTabSpec(tag) .setIndicator(tab.getText()); // And optional icon ... tabHost.addTab(spec); } // The other important method, newTab() is part of // the base implementation. }
ตอนนี้คุณมีการใช้งาน CompatTab
และ TabHelper
2 เวอร์ชัน ได้แก่ เวอร์ชันที่ใช้งานได้ในอุปกรณ์ที่ใช้ Android 3.0 ขึ้นไปและใช้ API ใหม่ และเวอร์ชันที่ใช้งานได้ในอุปกรณ์ที่ใช้ Android 2.0 ขึ้นไปและใช้ API เวอร์ชันเก่า บทเรียนถัดไปจะพูดถึงการนำไปใช้งานเหล่านี้ในแอปพลิเคชันของคุณ