android - 我可以在 Android 的自定义视图中包装 JetpackCompose 吗?
问题描述
在 Android UI 中,我们可以通过重载来创建自定义视图,View
如下所示。
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0)
: View(context, attrs, defStyleAttr) {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// Perform the needing drawing
if (isAttachedToWindow) invalidate()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val desiredWidth = suggestedMinimumWidth + paddingLeft + paddingRight
val desiredHeight = suggestedMinimumHeight + paddingTop + paddingBottom
setMeasuredDimension(View.resolveSize(desiredWidth, widthMeasureSpec),
View.resolveSize(desiredHeight, heightMeasureSpec))
}
}
我们可以包装JetpackCompose
在 this 中CustomView
,以便使用底层的 Drawing 它JetpackCompose
吗?
我检查了https://developer.android.com/jetpack/compose/interop/interop-apis,似乎没有说明。
解决方案
为了让它工作,我们可以有
class CustomComposeView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AbstractComposeView(context, attrs, defStyleAttr) {
@Composable
override fun Content() {
// JetpacCompose code here
}
}
在 XML 中,我们可以有这样的东西
<com.package.CustomComposeView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="700dp" />
推荐阅读
- javascript - IBM 对 https API 的 Web 操作调用不起作用
- rxjs - 如何通过单个订阅对 Observable 的首次发射进行特殊处理
- c# - 改变文化时如何只保留一个标准货币符号($)?
- google-maps - 我可以在一个项目中拥有多个 google api 密钥吗
- kubernetes - 无法从部署在工作节点上的 pod ping
- scheduler - 为什么气流 dag schedule_interval */1 * * * * 失败?
- asp.net - 为什么我在 ASP.NET 中有两个数据网格
- pandas - 如何从熊猫的另一列映射日期时间列的年份
- c# - 让我的 QR 扫描仪工作时遇到很多麻烦
- python - 在字典列表中查找匹配值并将字符串配对