首页 > 解决方案 > 在 Jetpack Compose 中使用自定义视图

问题描述

假设我正在使用一些旨在提供一些 UI 小部件的库。假设这个库提供了一个名为FancyButton.

另一方面,我有一个使用 Android Studio 4 创建的新项目,它允许我使用Empty Compose Activity.

问题是: 我应该如何将它添加FancyButton到视图堆栈中?可能吗?或者对于 Jetpack Compose,我只能使用专门为 Jetpack Compose 开发的组件。在这种情况下,AFAIK 我只能使用 Android 标准组件(Text、、MaterialTheme等)。

如果我尝试使用这样的东西:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        MaterialTheme {
            Greeting("Android")
            FancyButton(context, "Some text")
        }
    }
}

然后我得到这个错误:

e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath.

标签: androidandroid-jetpack-compose

解决方案


目前(截至0.1.0-dev04),对此没有很好的解决方案。将来,您将能够简单地将其称为FancyButton("Some text")context不需要)。

您可以在此处查看 Compose 代码中的演示。


推荐阅读