[Kotlin] DataBinding

장똑대·2021년 6월 23일
0

📄 build.gradle(Module)

plugins {
    ...
    id 'kotlin-kapt'
}

android {
    ...
    dataBinding{
        enabled = true
    }
}

dependencies {
	...
    kapt 'com.android.databinding:compiler:3.5.3'
}
  • 'kotlin-kapt' plugin / databinding{ enabled = true } / dependencies 추가


📄 activity_main.xml

<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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".news_list.MainActivity" />
        
</layout>
  • 'layout' 태그로 전체를 감싸주기


📄 MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    }
}
profile
장똑대와 안드로이드

0개의 댓글