ViewBinding
ViewBinding與DataBinding差不多但是有一些不一樣的特性
1.使用ViewBinding不用加上<layout>標籤
2.不能與XML中的數據做綁定
3.構建時會比DataBinding還快
root:布局中最外層的ViewGroup
android {
buildFeatures {
viewBinding true
}
}
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.data.text = "Hello ViewBinding"
}
}