Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

kotlin - Android: Jetpack Compose and XML in Activity

  1. Someone can showme how add jetpack compose y xml in the same activity. An example please
question from:https://stackoverflow.com/questions/65648904/android-jetpack-compose-and-xml-in-activity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use a Compose in your XML file, you can add this to your layout file:

<androidx.compose.ui.platform.ComposeView
    android:id="@+id/my_composable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

and then, set the content:

findViewById<ComposeView>(R.id.my_composable).setContent {
    MaterialTheme {
        Surface {
            Text(text = "Hello!")
        }
    }
}

if you want the opposite, use an XML file in your compose, you can use this:

    AndroidView(
        viewBlock = { context: Context ->
            val view =
                LayoutInflater.from(context)
                    .inflate(R.layout.my_layout, null, false)

            val textView = view.findViewById<TextView>(R.id.text)
            // do whatever you want...
            view // return the viw
        },
        update = { view ->
            // Update view
        }
    )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...