我正在使用 ComposeView 在片段中创建 ComposeView,如下所示:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return ComposeView(requireContext()).apply {
setContent {
var hideKeyboard by remember { mutableStateOf(false) }
Box(modifier = Modifier
.fillMaxSize()
.clickable {
hideKeyboard = true
}) {
val backgroundDrawable =
viewModel.onViewCreatedStateFlow.collectAsState().value
if(backgroundDrawable != null) {
Image(
modifier = Modifier.fillMaxSize(),
contentDescription = "",
painter = painterResource(tv.pluto.library.resources.R.attr.drawableWindowBackground)
)
}...
}}
我得到一个 Resource Not Found 异常。正如您所注意到的,当前的 if 语句使用调用 onViewCreated 时发出的 backgroundDrawable。当我将该值传递给 painter 时,代码按预期工作。为什么我无法在事件发生时访问可组合项中的资源?我该如何完成这项工作?