@@ -22,11 +22,11 @@ android {
22
22
23
23
``` xml
24
24
<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 >
30
30
```
31
31
所生成的绑定类的名称就为ResultProfileBinding。此类具有两个字段:一个是名为name的TextView,另一个是名为button的Button。该布局中的ImageView没有ID,因此绑定类中不存在对它的引用。
32
32
@@ -36,11 +36,10 @@ android {
36
36
如果你希望在生成绑定类时忽略某个布局文件,可以将tools: viewBindingIgnore ="true"属性添加到相应布局文件的根视图中:
37
37
``` xml
38
38
<LinearLayout
39
- ...
40
- tools : viewBindingIgnore =" true" >
41
- ...
42
- </LinearLayout >
43
-
39
+ ...
40
+ tools : viewBindingIgnore =" true" >
41
+ ...
42
+ </LinearLayout >
44
43
```
45
44
46
45
### 2.代码中使用
@@ -204,12 +203,12 @@ fragment_order.xml内容如下:
204
203
```
205
204
这个时候即使我们对include指定了id,在ViewBinding中也不会像普通include那样生成获取对应id的变量。这种情况下,我们需要使用placeholder.xml自动生成的PlaceholderBinding类,然后调用它的bind()方法。
206
205
``` 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
+ }
213
212
```
214
213
215
214
### View Binding 的传统使用方式
@@ -413,7 +412,7 @@ ViewBinding与DataBinding均会生成可用于直接引用视图的绑定类。
413
412
- [ Make Android View Binding great with Kotlin] ( https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719 )
414
413
- [ How to Simplify your Android View Binding Delegation] ( https://medium.com/easyread/how-to-simplify-your-android-view-binding-delegation-d07812b2a616 )
415
414
416
-
415
+ ## 目录
417
416
418
417
- [ 上一篇:1.简介] ( https://github.com/CharonChui/AndroidNote/blob/master/Jetpack/architecture/1.%E7%AE%80%E4%BB%8B.md )
419
418
- [ 下一篇:3.Lifecycle简介] ( https://github.com/CharonChui/AndroidNote/blob/master/Jetpack/architecture/3.Lifecycle%E7%AE%80%E4%BB%8B.md )
0 commit comments