ย้ายข้อมูลจากการสังเคราะห์ Kotlin ไปยังการเชื่อมโยงมุมมอง Jetpack
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เลิกใช้งานส่วนขยาย Android ของ Kotlin แล้ว ซึ่งหมายความว่าการใช้ Kotlin
ระบบไม่รองรับการสังเคราะห์สำหรับการเชื่อมโยงมุมมองอีกต่อไป หากแอปใช้ Kotlin
สังเคราะห์สําหรับการเชื่อมโยงมุมมอง ใช้คู่มือนี้เพื่อย้ายข้อมูลไปยังการเชื่อมโยงมุมมองของ Jetpack
หากแอปยังไม่ได้ใช้ Kotlin สังเคราะห์สำหรับการเชื่อมโยงมุมมอง โปรดดู View
การเชื่อมโยงสำหรับข้อมูลการใช้งานพื้นฐาน
อัปเดตไฟล์ Gradle
การเชื่อมโยงมุมมอง Jetpack จะเปิดใช้แบบโมดูลต่อโมดูลเช่นเดียวกับ Android Extensions
พื้นฐาน สำหรับแต่ละโมดูลที่ใช้การเชื่อมโยงข้อมูลพร็อพเพอร์ตี้ ให้ตั้งค่าบิลด์ viewBinding
เป็น true
ในไฟล์ build.gradle
ระดับโมดูล
ดึงดูด
android {
...
buildFeatures {
viewBinding true
}
}
Kotlin
android {
...
buildFeatures {
viewBinding = true
}
}
หากแอปไม่ได้ใช้ Parcelable
ใหม่ ให้นำบรรทัดที่เปิดใช้ส่วนขยาย Android ของ Kotlin ออก
ดึงดูด
plugins {
id 'kotlin-android-extensions'
}
Kotlin
plugins {
kotlin("android.extensions")
}
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการเปิดใช้การเชื่อมโยงมุมมองในโมดูล โปรดดูการตั้งค่า
วิธีการ
อัปเดตกิจกรรมและคลาสส่วนย่อย
เมื่อใช้การเชื่อมโยงมุมมอง Jetpack ระบบจะสร้างคลาสการเชื่อมโยงสำหรับไฟล์เลย์เอาต์ XML แต่ละไฟล์
ที่โมดูลมีด้วย ชื่อของคลาสการเชื่อมโยงนี้คือชื่อของ XML
ในกรณีของ Pascal ที่มีคำว่า Binding ต่อท้าย ตัวอย่างเช่น หาก
ชื่อไฟล์เลย์เอาต์คือ result_profile.xml
ซึ่งเป็นชื่อของที่สร้างขึ้น
คลาสการผูกคือ ResultProfileBinding
เพื่อใช้คลาสการเชื่อมโยงที่สร้างขึ้นแทนพร็อพเพอร์ตี้สังเคราะห์เพื่อ
อ้างอิงมุมมอง เปลี่ยนแปลงกิจกรรมและคลาสส่วนย่อยโดยการดำเนินการ
ดังต่อไปนี้:
นำการนำเข้าทั้งหมดออกจาก kotlinx.android.synthetic
เพิ่มอินสแตนซ์ของคลาสการเชื่อมโยงที่สร้างขึ้นสำหรับกิจกรรม หรือ
ที่จะใช้
เปลี่ยนการอ้างอิงมุมมองทั้งหมดเพื่อใช้อินสแตนซ์คลาสการเชื่อมโยงแทน
คุณสมบัติที่ผ่านการสังเคราะห์:
// Reference to "name" TextView using synthetic properties.
name.text = viewModel.nameString
// Reference to "name" TextView using the binding class instance.
binding.name.text = viewModel.nameString
ดูข้อมูลเพิ่มเติมได้ที่ส่วนการใช้งานใน
ในคู่มือการเชื่อมโยงมุมมอง
แนะนำสำหรับคุณ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Migrate from Kotlin synthetics to Jetpack view binding\n\nKotlin Android Extensions is deprecated, which means that using Kotlin\nsynthetics for view binding is no longer supported. If your app uses Kotlin\nsynthetics for view binding, use this guide to migrate to Jetpack view binding.\n\nIf your app doesn't already use Kotlin synthetics for view binding, see [View\nbinding](/topic/libraries/view-binding) for basic usage information.\n\nUpdate the Gradle file\n----------------------\n\nLike Android Extensions, Jetpack view binding is enabled on a module-by-module\nbasis. For each module that uses view binding, set the `viewBinding` build\noption to `true` in the module-level `build.gradle` file: \n\n### Groovy\n\n```groovy\nandroid {\n ...\n buildFeatures {\n viewBinding true\n }\n}\n```\n\n### Kotlin\n\n```kotlin\nandroid {\n ...\n buildFeatures {\n viewBinding = true\n }\n}\n```\n\nIf your app doesn't use [`Parcelable`](/reference/android/os/Parcelable)\nfeatures, remove the line that enables Kotlin Android Extensions: \n\n### Groovy\n\n```groovy\nplugins {\n id 'kotlin-android-extensions'\n}\n```\n\n### Kotlin\n\n```kotlin\nplugins {\n kotlin(\"android.extensions\")\n}\n```\n| **Note:** If your app uses `Parcelable` features, switch to using the standalone `kotlin-parcelize` Gradle plugin described in [Parcelable implementation generator](/kotlin/parcelize).\n\nTo learn more about enabling view binding in a module, see [Setup\ninstructions](/topic/libraries/view-binding#setup).\n\nUpdate activity and fragment classes\n------------------------------------\n\nWith Jetpack view binding, a binding class is generated for each XML layout file\nthat the module contains. The name of this binding class is the name of the XML\nfile in Pascal case with the word *Binding* added at the end. For example, if\nthe name of the layout file is `result_profile.xml`, the name of the generated\nbinding class is `ResultProfileBinding`.\n\nTo use the generated binding classes instead of synthetic properties to\nreference views, change your activity and fragment classes by doing the\nfollowing:\n\n1. Remove all imports from `kotlinx.android.synthetic`.\n\n2. Inflate an instance of the generated binding class for the activity or\n fragment to use.\n\n - For activities, follow the instructions in [Use view binding in\n activities](/topic/libraries/view-binding#activities) to inflate an instance in your activity's [`onCreate()`](/reference/kotlin/android/app/Activity#oncreate) method.\n - For fragments, follow the instructions in [Use view binding in\n fragments](/topic/libraries/view-binding#fragments) to inflate an instance in your fragment's [`onCreateView()`](/reference/kotlin/androidx/fragment/app/Fragment#oncreateview) method.\n3. Change all view references to use the binding class instance instead of\n synthetic properties:\n\n // Reference to \"name\" TextView using synthetic properties.\n name.text = viewModel.nameString\n\n // Reference to \"name\" TextView using the binding class instance.\n binding.name.text = viewModel.nameString\n\nTo learn more, see the [Usage](/topic/libraries/view-binding#usage) section in\nthe view binding guide.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [View binding](/topic/libraries/view-binding)\n- [Paging library overview](/topic/libraries/architecture/paging/v3-overview)\n- [Test your Paging implementation](/topic/libraries/architecture/paging/test)"]]