在模塊中啓用視圖綁定之後,系統會爲該模塊中的每個 XML 佈局文件生成一個綁定類。綁定類的實例包含對在相應佈局中具有 ID 的所有視圖的直接引用(替代findViewById)。
1. 啓用
## 1.1 可按模塊啓用。
要在某個模塊中啓用視圖綁定,請將 viewBinding
元素添加到其 build.gradle
文件中
android {
...
viewBinding {
enabled = true
}
}
1.2 在生成綁定類時忽略某個佈局文件
請將 tools:viewBindingIgnore="true"
屬性添加到相應佈局文件的根視圖中:
<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>
2. 用法
爲某個模塊啓用視圖綁定功能後,系統會爲該模塊中包含的每個 XML 佈局文件各生成一個綁定類。每個綁定類均包含對根視圖以及具有 ID 的所有視圖的引用。系統會通過以下方式生成綁定類的名稱:將 XML 文件的名稱轉換爲駝峯式大小寫,並在末尾添加“Binding”一詞。
比如__result_profile.xml__
<LinearLayout ... >
<TextView android:id="@+id/name" />
<ImageView android:cropToPadding="true" />
<Button android:id="@+id/button"
android:background="@drawable/rounded_button" />
</LinearLayout>
生成的綁定類名爲__ResultProfileBinding__
__ResultProfileBinding.inflate()__返回綁定類的實例
調用__setContentView()__,從而將該綁定類的根視圖作爲參數進行傳遞,以使它成爲屏幕上的活動視圖。
private lateinit var binding: ResultProfileBinding
@Override
fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
//設置活動視圖
binding = ResultProfileBinding.inflate(layoutInflater)
setContentView(binding.root)
}
__Fragment綁定視圖__示例:
佈局文件layout/fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InflateFragment">
<TextView
android:id="@+id/textViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
示例1(可選構造函數)
/**
* View Binding example with a fragment that uses the alternate constructor for inflation and
* [onViewCreated] for binding.
*/
class BindFragment : Fragment(R.layout.fragment_blank) {
// Scoped to the lifecycle of the fragment's view (between onCreateView and onDestroyView)
private var fragmentBlankBinding: FragmentBlankBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentBlankBinding.bind(view)
fragmentBlankBinding = binding
binding.textViewFragment.text = getString(string.hello_from_vb_bindfragment)
}
override fun onDestroyView() {
// Consider not storing the binding instance in a field, if not needed.
fragmentBlankBinding = null
super.onDestroyView()
}
}
示例2(主構造函數)
/**
* View Binding example with a fragment that uses the traditional constructor and [onCreateView] for
* inflation.
*/
class InflateFragment : Fragment() {
// Scoped to the lifecycle of the fragment's view (between onCreateView and onDestroyView)
private var fragmentBlankBinding: FragmentBlankBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val binding = FragmentBlankBinding.inflate(inflater, container, false)
fragmentBlankBinding = binding
//binding.textViewFragment.text = getString(R.string.hello_from_vb_inflatefragment)
return binding.root
}
override fun onDestroyView() {
// Consider not storing the binding instance in a field, if not needed.
fragmentBlankBinding = null
super.onDestroyView()
}
}
3. 與 findViewById 的區別
視圖綁定具有一些很顯著的優點:
- Null 安全:由於視圖綁定會創建對視圖的直接引用,因此不存在因視圖 ID 無效而引發 Null 指針異常的風險。此外,如果視圖僅出現在佈局的某些配置中,則綁定類中包含其引用的字段會使用
@Nullable
標記。 - 類型安全:每個綁定類中的字段均具有與它們在 XML 文件中引用的視圖相匹配的類型。這意味着不存在發生類轉換異常的風險。
這些差異意味着佈局和代碼之間的不兼容性可能會導致編譯版本在編譯時(而非運行時)失敗。
4. 與數據綁定庫的區別
視圖綁定和數據綁定庫均會生成可用於直接引用視圖的綁定類。不過,這兩者之間存在明顯差異:
- 數據綁定庫僅處理使用 代碼創建的數據綁定佈局。
- 視圖綁定不支持佈局變量或佈局表達式,因此它不能用於在 XML 中將佈局與數據綁定。