@@ -22,11 +22,11 @@ android {
2222
2323``` xml
2424<LinearLayout ... >
25- <TextView android : id =" @+id/name" />
26- <ImageView android : cropToPadding =" true" />
27- <Button android : id =" @+id/button"
28- android : background =" @drawable/rounded_button" />
29- </LinearLayout >
25+ <TextView android : id =" @+id/name" />
26+ <ImageView android : cropToPadding =" true" />
27+ <Button android : id =" @+id/button"
28+ android : background =" @drawable/rounded_button" />
29+ </LinearLayout >
3030```
3131所生成的绑定类的名称就为ResultProfileBinding。此类具有两个字段:一个是名为name的TextView,另一个是名为button的Button。该布局中的ImageView没有ID,因此绑定类中不存在对它的引用。
3232
@@ -36,11 +36,10 @@ android {
3636如果你希望在生成绑定类时忽略某个布局文件,可以将tools: viewBindingIgnore ="true"属性添加到相应布局文件的根视图中:
3737``` xml
3838<LinearLayout
39- ...
40- tools : viewBindingIgnore =" true" >
41- ...
42- </LinearLayout >
43-
39+ ...
40+ tools : viewBindingIgnore =" true" >
41+ ...
42+ </LinearLayout >
4443```
4544
4645### 2.代码中使用
@@ -204,12 +203,12 @@ fragment_order.xml内容如下:
204203```
205204这个时候即使我们对include指定了id,在ViewBinding中也不会像普通include那样生成获取对应id的变量。这种情况下,我们需要使用placeholder.xml自动生成的PlaceholderBinding类,然后调用它的bind()方法。
206205``` kotlin
207- override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
208- binding = FragmentOrderBinding .inflate(layoutInflater, container, false )
209- placeholderBinding = PlaceholderBinding .bind(binding.root)
210- placeholderBinding.tvPlaceholder.text = getString(R .string.please_wait)
211- return binding.root
212- }
206+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
207+ binding = FragmentOrderBinding .inflate(layoutInflater, container, false )
208+ placeholderBinding = PlaceholderBinding .bind(binding.root)
209+ placeholderBinding.tvPlaceholder.text = getString(R .string.please_wait)
210+ return binding.root
211+ }
213212```
214213
215214### View Binding 的传统使用方式
@@ -413,7 +412,7 @@ ViewBinding与DataBinding均会生成可用于直接引用视图的绑定类。
413412- [ Make Android View Binding great with Kotlin] ( https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719 )
414413- [ How to Simplify your Android View Binding Delegation] ( https://medium.com/easyread/how-to-simplify-your-android-view-binding-delegation-d07812b2a616 )
415414
416-
415+ ## 目录
417416
418417- [ 上一篇:1.简介] ( https://github.com/CharonChui/AndroidNote/blob/master/Jetpack/architecture/1.%E7%AE%80%E4%BB%8B.md )
419418- [ 下一篇:3.Lifecycle简介] ( https://github.com/CharonChui/AndroidNote/blob/master/Jetpack/architecture/3.Lifecycle%E7%AE%80%E4%BB%8B.md )
0 commit comments