일반적으로 compose를 기존 android 앱으로 이전 하는 방법에는 여러가지가 있다. 우선 일반적인 2가지가 있는데
이제 한번 android view에서 compose로 마이그레이션을 해보자
우선 compose 로 이전하게 된다면 전체적인 구조는 그대로 둔다. 이렇게 하면 Compose와 view의 공존 migration 전략을 따를수가 있다.
먼저 xml에 들어가 전체적인 덩어리 코드를 삭제 하는 것이 아니라 일부 코드를 삭제 하고 composeview로 변환 시킨다.
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
삭제 했던 xml 코드
<TextView
android:id="@+id/plant_detail_name"
...
android:layout_marginStart="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"
android:gravity="center_horizontal"
android:text="@{viewModel.plant.name}"
android:textAppearance="?attr/textAppearanceHeadline5"
... />
compose 코드로 변환
@Composable
private fun PlantName(name: String) {
Text(
text = name,
style = MaterialTheme.typography.h5,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.margin_small))
.wrapContentWidth(Alignment.CenterHorizontally)
)
}
Text
의 스타일은 XML 코드에서 textAppearanceHeadline5
는 MaterialTheme.typography.h5
이다.fillMaxWidth
수정자는 XML 코드의 android:layout_width="match_parent"
에 해당.