1.在Activity或者Fragment中全部使用Compose來搭建UI
Use Compose in Activity
class ExampleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { // In here, we can call composables!
MaterialTheme {
Greeting(name = "compose")
}
}
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
Use Compose in Fragment
class PureComposeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
MaterialTheme {
Text("Hello Compose!")
}
}
}
}
}
在View中使用Compose
ComposeView內(nèi)嵌在Xml中:
一個平平無奇的xml布局文件中加入ComposeView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello from XML layout" />
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
使用的時候, 先根據(jù)id查找出來, 再setContent:
class ComposeViewInXmlActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_compose_view_in_xml)
findViewById<ComposeView>(R.id.compose_view).setContent {
// In Compose world
MaterialTheme {
Text("Hello Compose!")
}
}
}
}
動態(tài)添加ComposeView
在代碼中使用addView()來添加View對于ComposeView來說也同樣適用
class ComposeViewInViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(LinearLayout(this).apply {
orientation = VERTICAL
addView(ComposeView(this@ComposeViewInViewActivity).apply {
id = R.id.compose_view_x
setContent {
MaterialTheme {
Text("Hello Compose View 1")
}
}
})
addView(TextView(context).apply {
text = "I'm am old TextView"
})
addView(ComposeView(context).apply {
id = R.id.compose_view_y
setContent {
MaterialTheme {
Text("Hello Compose View 2")
}
}
})
})
}
}
這里在LinearLayout中添加了三個child: 兩個ComposeView中間還有一個TextView.
起到橋梁作用的ComposeView
是一個ViewGroup, 它本身是一個View, 所以可以混進(jìn)View的hierarchy tree里占位,
它的setContent()方法開啟了Compose世界的大門, 在這里可以傳入composable的方法, 繪制UI.
在Compose中使用View
都用Compose搭建UI了, 什么時候會需要在其中內(nèi)嵌View呢?
1.要用的View還沒有Compose版本, 比如AdView, MapView, WebView.
2.有一塊之前寫好的UI, (暫時或者永遠(yuǎn))不想動, 想直接用.
3.用Compose實現(xiàn)不了想要的效果, 就得用View.
在Compose中加入Android View
@Composable
fun CustomView() {
val state = remember { mutableStateOf(0) }
//widget.Button
AndroidView(
factory = { ctx ->
//Here you can construct your View
android.widget.Button(ctx).apply {
text = "My Button"
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
setOnClickListener {
state.value++
}
}
},
modifier = Modifier.padding(8.dp)
)
//widget.TextView
AndroidView(factory = { ctx ->
//Here you can construct your View
TextView(ctx).apply {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
}
}, update = {
it.text = "You have clicked the buttons: " + state.value.toString() + " times"
})
}
這里的橋梁是AndroidView, 它是一個composable方法:
@Composable
fun <T : View> AndroidView(
factory: (Context) -> T,
modifier: Modifier = Modifier,
update: (T) -> Unit = NoOpUpdate
)
factory接收一個Context參數(shù), 用來構(gòu)建一個View.
update方法是一個callback, inflate之后會執(zhí)行, 讀取的狀態(tài)state值變化后也會被執(zhí)行.
在Compose中使用xml布局
上面提到的在Compose中使用AndroidView的方法, 對于少量的UI還行.
如果需要復(fù)用一個已經(jīng)存在的xml布局怎么辦?
不用怕, view binding登場了.
使用起來也很簡單:
1.首先你需要開啟View Binding.
buildFeatures {
compose true
viewBinding true
}
2.其次你需要一個xml的布局, 比如叫complex_layout.
3.然后添加一個Compose view binding的依賴: androidx.compose.ui:ui-viewbinding.
然后build一下, 生成binding類,
這樣就好了
@Composable
private fun ComposableFromLayout() {
AndroidViewBinding(ComplexLayoutBinding::inflate) {
sampleButton.setBackgroundColor(Color.GRAY)
}
}
其中ComplexLayoutBinding
是根據(jù)布局名字生成的類.
AndroidViewBinding
內(nèi)部還是調(diào)用了AndroidView
這個composable方法.
在Compose中顯示Fragment
這個場景聽上去有點奇葩, 因為Compose的設(shè)計理念, 貌似就是為了跟Fragment說再見.
在Compose構(gòu)建的UI中, 再找地方顯示一個Fragment, 有點新瓶裝舊酒的意思.
但是遇到的場景多了, 你沒準(zhǔn)真能遇上呢.
Fragment通過FragmentManager添加, 需要一個布局容器.
把上面ViewBinding的例子改改, 布局里加入一個fragmentContainer, 點擊顯示Fragment:
Column(Modifier.fillMaxSize()) {
Text("I'm a Compose Text!")
Button(
onClick = {
showFragment()
}
) {
Text(text = "Show Fragment")
}
ComposableFromLayout()
}
@Composable
private fun ComposableFromLayout() {
AndroidViewBinding(
FragmentContrainerBinding::inflate,
modifier = Modifier.fillMaxSize()
) {
showFragment()
}
}
private fun showFragment() {
supportFragmentManager
.beginTransaction()
.add(R.id.fragmentContainer, PureComposeFragment())
.commit()
}