728x90
- ๊ณตํต activity_main.xml ์ฝ๋
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- ๊ธฐ๋ณธ MainActivity.java ์ฝ๋
package com.devshin93.apktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- findViewById ์ ์ฉํ MainActivity.java ์ฝ๋
package com.devshin93.apktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
textView.setText("findViewById");
}
}
- View Binding ์ ํ์ํ configuration ์ถ๊ฐ build.gradle (:app)
Databinding ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ Android Gradle ํ๋ฌ๊ทธ์ธ๊ณผ ํจ๊ป ๋ฒ๋ค๋ก ์ ๊ณต๋ฉ๋๋ค. ๊ฐ๋ฐ์๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ข ์ ํญ๋ชฉ์ ์ ์ธํ ํ์๋ ์์ง๋ง, ์ฌ์ฉ ์ค์ ์ ํด์ผ ํฉ๋๋ค.
android {
...
// Android Studio 3.6 ๋ถํฐ 4.0 ์ด์ ๊น์ง
viewBinding {
enabled = true
}
// Android Studio 4.0 ๋ถํฐ
buildFeatures {
viewBinding true
}
}
- View Binding ์ ์ฉํ MainActivity.java ์ฝ๋
package com.devshin93.apktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.devshin93.apktest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.textView.setText("View Binding");
}
}
์ฐธ์กฐ : https://developer.android.com/topic/libraries/view-binding
์ฐธ์กฐ : https://developer.android.com/jetpack/androidx/releases/databinding?hl=ko
๋๊ธ