Android Studio version 3.3.2
app->build.gradle
android {
dataBinding{
enabled = true
}
}
第二步 gradle.properties
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
android.databinding.enableV2=true
Gradle-wrapper.properties文件
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
第三步编写一个 Activity
package com.androidpath.architecture.databinding
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.androidpath.R
class DataActivity : AppCompatActivity() {
private lateinit var binding: com.androidpath.databinding.ActivityDataBinding
private val myName: MyName = MyName("tom", "nick")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_data)
val textView = binding.tv as TextView
textView.setOnClickListener {
binding.myName=myName
Toast.makeText(this, "This is DataBinding", Toast.LENGTH_SHORT).show()
}
}
}
第四步:layout布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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:id="@+id/layout">
<data>
<variable
name="myName"
type="com.androidpath.architecture.databinding.MyName" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="click me"
android:textColor="@color/gray"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@={myName.name}"
android:textColor="@color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nickname"
tools:text="name" />
<TextView
android:id="@+id/nickname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:background="@android:color/holo_green_dark"
android:fontFamily="@font/roboto_thin"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:text="@={myName.nickname}"
android:textAlignment="center"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv"
tools:text="nickname" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
第五步 创建一个data class
package com.androidpath.architecture.databinding
data class MyName(var name:String="",var nickname:String="")