Jetpack Compose MotionLayout XML属性等效实现及Json参数列表查询
Jetpack Compose MotionLayout 相关问题解答
XML属性对应效果实现
你给出的两个XML属性的对应实现方式如下:
首先你给出的第二个XML属性存在拼写误差,正确属性应为motion:layout_constraintVertical_bias="1.0",作用是设置组件在垂直约束区间的偏移比例,1.0代表完全对齐底部约束;motion:layout_constrainedHeight="true"的作用是限制组件高度不会超过上下约束的边界,即使内容高度超出约束范围也会被裁剪。
1. ConstraintSet DSL 实现
在constrain代码块中直接设置对应属性即可,示例代码:
val motionConstraintSet = ConstraintSet { val targetComponent = createRefFor("target") constrain(targetComponent) { // 绑定上下约束 top.linkTo(parent.top) bottom.linkTo(parent.bottom) // 对应 motion:layout_constrainedHeight="true" constrainedHeight = true height = Dimension.wrapContent // 对应 motion:layout_constraintVertical_bias=1.0 verticalBias = 1f } }
2. JSON 格式 ConstraintSet 实现
JSON参数命名和原生XML属性完全一致,示例如下:
{ "target": { "layout_constrainedHeight": true, "layout_constraintVertical_bias": 1, "topToTop": "parent", "bottomToBottom": "parent", "height": "wrap" } }
JSON参数完整列表说明
目前官方没有对外发布单独的MotionLayout JSON参数完整列表,你可以直接对照Compose ConstraintSet DSL的参数命名,将驼峰命名转换为下划线格式、添加layout_前缀即可对应到JSON参数,几乎所有参数都和View体系MotionLayout的XML属性命名一致,可以直接对照使用。
内容的提问来源于stack exchange,提问作者Tonio
相关产品推荐
相关产品推荐

