我想从一个约束集制作一个Changebounds()
动画。
通常,我通过创建两个约束集来做到这一点:
private val originalState = ConstraintSet().apply {
clone(context, R.layout.layout_meta_syntactic)
}
private val expandedState = ConstraintSet().apply {
clone(context, R.layout.layout_meta_syntactic)
// Change some constraints
connect(
R.id.button, ConstraintSet.END,
R.id.foo_text, ConstraintSet.START
)
}
并用以下方式来回动画:
TransitionManager.beginDelayedTransition(id_of_my_component_in_fragment, transition)
originalState.applyTo(id_of_my_component_in_fragment)
但是现在我在要克隆的布局中使用了<merge>
标签。合并布局是扩展ConstraintLayout的复合组件的基础。
复合成分:
class MyCompoundView : ConstraintLayout {
// Omissions
inflate(context, R.layout.layout_meta_syntactic, this)
膨胀:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/some_id"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
// Views
当尝试以编程方式将布局克隆到约束集中时,我得到:
Caused by: android.view.InflateException: Binary XML file line #2: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
如何从这样的布局创建约束集?
答案 0 :(得分:1)
您有两个选择:
ConstraintLayout
根创建另一个(虚拟)布局文件,该文件包含带有include
标签的布局引用。ConstraintSet.clone(context, MyCompoundView(context))
。 ConstraintSet.clone(context, layoutRes)
实际上是非常粗糙的(source),在解决其布局参数以构建{{1]之前,它实际上从提供的布局文件(包括所有子视图)中夸大了新的ConstraintLayout
}}。
答案 1 :(得分:0)
解决方案是克隆复合组件ConstraintLayout
class MyCompoundView : ConstraintLayout {
// make sure to clone after inflating the layout to the component
originalState = ConstraintSet().apply {
clone(this@MyCompoundView)
}
expandedState = ConstraintSet().apply {
clone(this@MyCompoundView)
// Change some constraints
connect(
R.id.button, ConstraintSet.END,
R.id.foo_text, ConstraintSet.START
)
}
}