针对可折叠设备设计应用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。

在ConstraintLayout
中
2.1 版本中,我们添加了几项功能来帮助
管理可折叠设备,包括SharedValues
、
ReactiveGuide
,以及通过 MotionLayout
增强了对动画的支持。
共享值
我们在 ConstraintLayout
中添加了一种新机制来注入运行时值:
旨在用于系统级值,因为
“ConstraintLayout
”能够访问此值。
对于可折叠设备,我们可以使用这种机制
折叠边在运行时的位置:
Kotlin
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold)
Java
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold);
在自定义帮助程序中,您可以通过添加
更改:
Kotlin
val sharedValues: SharedValues = ConstraintLayout.getSharedValues()
sharedValues.addListener(mAttributeId, this)
Java
SharedValues sharedValues = ConstraintLayout.getSharedValues();
sharedValues.addListener(mAttributeId, this);
您可以查看 FoldableExperiments 示例
看看我们如何使用
Jetpack WindowManager 库和注入
放入 ConstraintLayout
。
Kotlin
inner class StateContainer : Consumer<WindowLayoutInfo> {
override fun accept(newLayoutInfo: WindowLayoutInfo) {
// Add views that represent display features
for (displayFeature in newLayoutInfo.displayFeatures) {
val foldFeature = displayFeature as? FoldingFeature
if (foldFeature != null) {
if (foldFeature.isSeparating &&
foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL
) {
// The foldable device is in tabletop mode
val fold = foldPosition(motionLayout, foldFeature)
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold)
} else {
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, 0);
}
}
}
}
}
Java
class StateContainer implements Consumer<WindowLayoutInfo> {
@Override
public void accept(WindowLayoutInfo newLayoutInfo) {
// Add views that represent display features
for (DisplayFeature displayFeature : newLayoutInfo.getDisplayFeatures()) {
if (displayFeature instanceof FoldingFeature) {
FoldingFeature foldFeature = (FoldingFeature)displayFeature;
if (foldFeature.isSeparating() &&
foldFeature.getOrientation() == FoldingFeature.Orientation.HORIZONTAL
) {
// The foldable device is in tabletop mode
int fold = foldPosition(motionLayout, foldFeature);
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold);
} else {
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, 0);
}
}
}
}
}
fireNewValue()
将表示值的 ID 作为第一个参数,
要作为第二个参数注入的值。
ReactiveGuide

在布局中利用 SharedValue
的一种方式,无需
使用 ReactiveGuide
帮助程序。这将根据
关联时间:SharedValue
。
<androidx.constraintlayout.widget.ReactiveGuide
android:id="@+id/fold"
app:reactiveGuide_valueId="@id/fold"
android:orientation="horizontal" />
然后,您可以像使用一般准则一样使用它。
MotionLayout
适用于可折叠设备
我们在 2.1 版的 MotionLayout
中添加了几项有助于变形的功能
状态,这对可折叠设备特别有用。
必须处理不同可能布局之间的动画。
可折叠设备有两种方法:
- 在运行时,更新当前布局 (
ConstraintSet
) 以显示或隐藏
折叠。
- 针对所需的每种可折叠状态使用单独的
ConstraintSet
支持(closed
、folded
或 fully open
)。
为 ConstraintSet
添加动画效果
2.1 版中添加了 MotionLayout
中的 updateStateAnimate()
函数
发布:
Kotlin
fun updateStateAnimate(stateId: Int, set: ConstraintSet, duration: Int)
Java
void updateStateAnimate(int stateId, ConstraintSet set, int duration);
该函数会在更新指定的
ConstraintSet
,而不是立即更新(您可以通过
updateState(stateId, constraintset)
)。这样,您就可以在
动态运行,具体取决于变化,例如您当前所处的可折叠状态。
MotionLayout
中的 ReactiveGuide
ReactiveGuide
还支持在
MotionLayout
:
第一个代码将修改当前的 ConstraintSet
,并为变化添加动画效果
。第二个代码将采用 ReactiveGuide
的新值
应用于 MotionLayout
中所有 ConstraintSet
的位置。典型的
可折叠设备应使用表示折叠位置的 ReactiveGuide
,
相对于 ReactiveGuide
设置布局元素。
使用多个 ConstraintSet
表示可折叠状态
不必更新当前的 MotionLayout
状态,
为了支持可折叠设备,您的界面是创建特定的单独的状态(包括
closed
、folded
和 fully open
)。

在这种情况下,您可能仍需要使用 ReactiveGuide
来表示
但您可以拥有更多控制权(与
更新当前 ConstraintSet
时的动画),说明每种状态是如何
过渡到另一个主题。
如果使用此方法,在 DeviceState
监听器中,您只需指示
MotionLayout
,通过
MotionLayout.transitionToState(stateId)
方法。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Designing for foldables\n\nIn the [`ConstraintLayout`](/reference/androidx/constraintlayout/widget/ConstraintLayout)\n2.1 release, several features were added to help\nmanage [foldable devices](/guide/topics/ui/foldables), including [`SharedValues`](/reference/androidx/constraintlayout/widget/SharedValues),\n[`ReactiveGuide`](https://github.com/androidx/constraintlayout/blob/d89c45dbb74bf19ad4834198a04af306696357bc/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/widget/ReactiveGuide.java), and enhanced support for animation with [`MotionLayout`](/reference/androidx/constraintlayout/motion/widget/MotionLayout).\n\nShared Values\n-------------\n\nWe added a new mechanism to inject runtime values in `ConstraintLayout` --\nthis is intended to be used for system-wide values, as all instances of\n`ConstraintLayout` are able to access the value.\n\nIn the context of foldable devices, we can use this mechanism to inject the\nposition of the fold at runtime: \n\n### Kotlin\n\n```kotlin\nConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold)\n```\n\n### Java\n\n```java\nConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold);\n```\n\nIn a custom helper, you can access the shared values by adding a listener for\nany changes: \n\n### Kotlin\n\n```kotlin\nval sharedValues: SharedValues = ConstraintLayout.getSharedValues()\nsharedValues.addListener(mAttributeId, this)\n```\n\n### Java\n\n```java\nSharedValues sharedValues = ConstraintLayout.getSharedValues();\nsharedValues.addListener(mAttributeId, this);\n```\n\nYou can look at the [FoldableExperiments example](https://github.com/androidx/constraintlayout/blob/main/projects/FoldableExperiments/app/src/main/java/com/example/experiments/MainActivity.kt)\nto see how we capture the position of the fold using the\n[Jetpack WindowManager](/jetpack/androidx/releases/window) library and inject\nthe position into `ConstraintLayout`. \n\n### Kotlin\n\n```kotlin\ninner class StateContainer : Consumer\u003cWindowLayoutInfo\u003e {\n\n override fun accept(newLayoutInfo: WindowLayoutInfo) {\n\n // Add views that represent display features\n for (displayFeature in newLayoutInfo.displayFeatures) {\n val foldFeature = displayFeature as? FoldingFeature\n if (foldFeature != null) {\n if (foldFeature.isSeparating &&\n foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL\n ) {\n // The foldable device is in tabletop mode\n val fold = foldPosition(motionLayout, foldFeature)\n ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold)\n } else {\n ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, 0);\n }\n }\n }\n }\n}\n```\n\n### Java\n\n```java\nclass StateContainer implements Consumer\u003cWindowLayoutInfo\u003e {\n\n @Override\n public void accept(WindowLayoutInfo newLayoutInfo) {\n\n // Add views that represent display features\n for (DisplayFeature displayFeature : newLayoutInfo.getDisplayFeatures()) {\n if (displayFeature instanceof FoldingFeature) {\n FoldingFeature foldFeature = (FoldingFeature)displayFeature;\n if (foldFeature.isSeparating() &&\n foldFeature.getOrientation() == FoldingFeature.Orientation.HORIZONTAL\n ) {\n // The foldable device is in tabletop mode\n int fold = foldPosition(motionLayout, foldFeature);\n ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold);\n } else {\n ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, 0);\n }\n }\n }\n }\n}\n```\n\n`fireNewValue()` takes an ID representing the value as the first parameter and\nthe value to inject as the second parameter.\n\n`ReactiveGuide`\n---------------\n\nOne way to take advantage of a `SharedValue` in a layout, without having to\nwrite any code, is to use the [`ReactiveGuide`](https://github.com/androidx/constraintlayout/blob/main/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/widget/ReactiveGuide.java)\nhelper. This will position a horizontal or vertical guideline according to the\nlinked `SharedValue`. \n\n \u003candroidx.constraintlayout.widget.ReactiveGuide\n android:id=\"@+id/fold\"\n app:reactiveGuide_valueId=\"@id/fold\"\n android:orientation=\"horizontal\" /\u003e\n\nIt can then be used as a you would with a normal guideline.\n\n`MotionLayout` for foldables\n----------------------------\n\nWe added several features in `MotionLayout` in 2.1 that helps morphing\nstate -- something particularly useful for foldables, as we typically\nhave to handle animating between the different possible layouts.\n\nThere are two approaches available for foldables:\n\n- At runtime, update your current layout (`ConstraintSet`) to show or hide the fold.\n- Use a separate `ConstraintSet` for each of the foldable states you want to support (`closed`, `folded`, or `fully open`).\n\n### Animating a `ConstraintSet`\n\nThe function `updateStateAnimate()` in `MotionLayout` was added in the 2.1\nrelease: \n\n### Kotlin\n\n```kotlin\nfun updateStateAnimate(stateId: Int, set: ConstraintSet, duration: Int)\n```\n\n### Java\n\n```java\nvoid updateStateAnimate(int stateId, ConstraintSet set, int duration);\n```\n\nThis function will automatically animate the changes when updating a given\n`ConstraintSet` instead of doing an immediate update (which you can do with\n`updateState(stateId, constraintset)`). This allows you to update your UI on\nthe fly, depending on changes, such as which foldable state you are in.\n\n### `ReactiveGuide` inside a `MotionLayout`\n\n`ReactiveGuide` also supports two useful attributes when used inside a\n`MotionLayout`:\n\n- `app:reactiveGuide_animateChange=\"true|false\"`\n\n- `app:reactiveGuide_applyToAllConstraintSets=\"true|false\"`\n\nThe first one will modify the current `ConstraintSet` and animate the change\nautomatically. The second one will apply the new value of the `ReactiveGuide`\nposition to all `ConstraintSet`s in the `MotionLayout`. A typical approach for\nfoldables would be to use a `ReactiveGuide` representing the fold position,\nsetting up your layout elements relative to the `ReactiveGuide`.\n\n### Using multiple `ConstraintSet`s to represent foldable state\n\nInstead of updating the current `MotionLayout` state, another way to architect\nyour UI to support foldables is to create specific separate states (including\n`closed`, `folded`, and `fully open`).\n\nIn this scenario, you might still want to use a `ReactiveGuide` to represent the\nfold, but you would have a lot more control (compared to the automated\nanimation when updating the current `ConstraintSet`) on how each state would\ntransition into another.\n\nWith this approach, in your `DeviceState` listener, you would simply direct the\n`MotionLayout` to transition to specific states through the\n[`MotionLayout.transitionToState(stateId)`](/reference/androidx/constraintlayout/motion/widget/MotionLayout#transitionToState(int))\nmethod."]]