Android Dev/Compose / / 2021. 12. 6. 00:25

activity/fragment에서 Compose 호출

 

1. 레이아웃에서 아래와 같이 선언합니다

 

2.

 


        binding = DataBindingUtil.inflate<FragmentMainBinding>(inflater, R.layout.fragment_main, container, false).apply {
//            lifecycleOwner = viewLifecycleOwner
        }

        binding.composeView.setContent {
            VlmWorldTheme {
                // A surface container using the 'background' color from the theme
//                Surface(color = MaterialTheme.colors.background) {
                    EnterPoint(userViewModel)
//                }
            }
        }

 

이와 같이 호출하면 사용가능합니다

 

변수 연결 예제

 


var checkable = mutableStateOf(false)
binding.m7compose.setOnClickListener {
    checkable.value = !checkable.value
}
binding.composeView.setContent {
    val composeAble by checkable
    if(composeAble)
        Text("helloTest")
}

 




<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/m7clo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginBottom="1dp"
    android:elevation="6dp"

    android:orientation="horizontal"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 



<TextView
    android:id="@+id/m7compose"
    style="@style/text_view_contents_item_setting"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="1dp"
    android:text="컴포즈테스트"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유