MainActivity.kt
// Compose 안에서 XML 사용
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DefaultTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Column {
Text(text = "여기는 Compose1", fontSize = 30.sp)
// XML
AndroidView(factory = {context ->
val view = LayoutInflater.from(context).inflate(R.layout.temp_xml, null, false)
view.findViewById<TextView>(R.id.tempText).setOnClickListener {
Toast.makeText(context, "이것은 토스트", Toast.LENGTH_LONG).show()
}
view
})
Text(text = "여기는 Compose2", fontSize = 30.sp)
}
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
DefaultTheme {
Greeting("Android")
}
}
temp_xml.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tempText"
android:text="여기는 XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
</LinearLayout>