【问题标题】:Call Jetpack Compose element from a java file从 java 文件中调用 Jetpack Compose 元素
【发布时间】:2022-08-17 00:13:07
【问题描述】:

我正在尝试使用 databindingsetContent() 在活动中现有的 XML 中使用 Jetpack Compose UI 元素。 这是 xml 元素:

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

我在 kotlin 活动文件中使用了这个语法,它运行顺利:

binding.ComposeView.setContent{
    MainActionButtonKt.MainActionButton(true, R.string.complete_job, R.drawable.ic_complete_btn_icon, false);
}

(got it from android\'s duc)

java活动文件的等价物是什么?我试图这样做:

binding.saveBtnCompose.setContent((composer, integer) -> {
     MainActionButton(true, R.string.complete_job, R.drawable.ic_complete_btn_icon, false);
     return null;
});

但我得到一个编译错误:

required: boolean, int, Integer, boolean, Composer, int, int
found:    boolean, int, int,     boolean

reason: actual and formal argument lists differ in length

我究竟做错了什么?谢谢 !

标签: java kotlin android-jetpack-compose


【解决方案1】:

您可以使用。

在其他任何地方创建一个 kotlin 函数

fun setContent(composeView:ComposeView, composeparam1: Boolean){
    composeView.setContent {
        CallYourComposeFunction(composeparam1)
    }
}

然后通过从 xml 提供 composeView 从您的 java 代码中调用它。

【讨论】:

    猜你喜欢
    • 2022-11-01
    • 2022-12-12
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多