🔗【Jetpack】Android ViewBinding 視圖綁定 範例🎯
Android ViewBinding 是 Google 推出的用於便利開發 Android 應用的一種功能,它可以有效簡化訪問 Layout 的繁瑣操作,為開發者提供了更高的表現力和可維護性。
ViewBinding 通過向開發者提供所有 UI 元素的實例來減少代碼的重複量,進而減少與界面連接的時間和精力成本,同時降低崩潰的概率和出錯的可能性。
本文介紹了 Android ViewBinding 的功能、优点以及在框架中的應用,剖析了 ViewBinding 技術對 Android 開發的影響和帶來的優勢。
文章目錄
- ViewBinding & Fragment KTX 導入
- ViewBinding Layout
- ViewBinding MainActivity
- ViewBinding InflateFragment
- ViewBinding BindFragment
- Developer Documents ViewBinding
1.ViewBinding & Fragment KTX 導入
build.gradle
android {
buildFeatures {
viewBinding = true
}
}
dependencies {
def fragment_version = "1.5.5"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
}
2.ViewBinding Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bind"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/inflate"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.66" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.33" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.ViewBinding MainActivity
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
binding.name.text = "Activity"
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
supportFragmentManager.commit {
add<BindFragment>(R.id.bind)
add<InflateFragment>(R.id.inflate)
setReorderingAllowed(true)
addToBackStack("name")
}
}
}
4.ViewBinding InflateFragment
InflateFragment.kt
class InflateFragment : Fragment() {
private var fragmentInflateBinding: FragmentInflateBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
val binding = FragmentInflateBinding.inflate(inflater, container, false)
fragmentInflateBinding = binding
binding.name.text = "InflateFragment"
return binding.root
}
override fun onDestroyView() {
fragmentInflateBinding = null
super.onDestroyView()
}
}
5.ViewBinding BindFragment
BindFragment.kt
class BindFragment : Fragment(R.layout.fragment_bind) {
private var fragmentBindBinding: FragmentBindBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentBindBinding.bind(view)
fragmentBindBinding = binding
binding.name.text = "BindFragment"
}
override fun onDestroyView() {
fragmentBindBinding = null
super.onDestroyView()
}
}
6.Developer Documents ViewBinding
Open in Documents ViewBinding