WindowAdaptiveInfo:用窗口而非设备分类 · AndroidX 源码指南
AAndroidX 源码指南
自适应布局
自适应布局 · androidx.window

WindowAdaptiveInfo:用窗口而非设备分类Window 1.6.0-alpha05

使用 V2 尺寸级别和 Posture 驱动 Compose 自适应决策。

最后更新 2026-08-01

复制即用

@Composable
fun AdaptiveScreen() {
    val adaptiveInfo = currentWindowAdaptiveInfoV2()
    val widthClass = adaptiveInfo.windowSizeClass.windowWidthSizeClass
    val heightClass = adaptiveInfo.windowSizeClass.windowHeightSizeClass
    val posture = adaptiveInfo.windowPosture

    ScreenContent(
        widthClass = widthClass,
        heightClass = heightClass,
        posture = posture,
    )
}

注意:当前源码推荐 currentWindowAdaptiveInfoV2();旧 currentWindowAdaptiveInfo() 已弃用。

宽度尺寸级别

级别范围(dp)典型布局
Compact0–599手机单栏
Medium600–839平板小/折叠展开
Expanded840–1199平板/桌面双栏
Large1200–1599大平板/桌面
Extra Large1600+宽屏桌面

Compose 用法

when (widthClass) {
    WindowWidthSizeClass.COMPACT -> SinglePane()
    WindowWidthSizeClass.MEDIUM -> TwoPane()
    else -> ListDetailPane()
}

Posture(折叠设备)

when (posture) {
    is Posture.Folded -> // 折叠状态
    is Posture.HalfOpened -> // 半开(铰链)
    is Posture.FlatSeparated -> // 展开(铰链分隔)
    is Posture.Flat -> // 完全展开
}

关键原则

  • 尺寸级别是布局输入,不是设备身份——不要用 if (isTablet) 判断。
  • 不要缓存一次结果:Compose 入口会随窗口变化(分屏、旋转)自动更新。
  • 只在需要自定义断点时读 LocalWindowInfo.current.containerSize

与 NavigationSuite 配合

NavigationSuiteScaffold(
    navigationSuiteItems = { ... },
)

NavigationSuite 内部用 WindowAdaptiveInfo 自动切换底部栏 / 侧边 rail——分屏旋转时自动响应。

常见陷阱

  • 启动时算一次:窗口变化后布局不更新。
  • 用设备名判断:平板、折叠、桌面都在变,只有窗口尺寸稳定。
  • 旧 APIcurrentWindowAdaptiveInfo() 已弃用,用 V2。
  • 忽略高度:高度级别也重要(横屏手机)。

复制即用

@Composable
fun ResponsiveContent() {
    val adaptiveInfo = currentWindowAdaptiveInfoV2()
    when (adaptiveInfo.windowSizeClass.windowWidthSizeClass) {
        WindowWidthSizeClass.COMPACT -> CompactLayout()
        WindowWidthSizeClass.MEDIUM -> MediumLayout()
        else -> ExpandedLayout()
    }
}

要点

  • currentWindowAdaptiveInfoV2() 是当前推荐入口;组合尺寸 + 姿态。
  • 按窗口尺寸级别而不是设备名做布局决策。
  • 不缓存、随窗口更新;分屏/旋转自动响应。
  • 自定义断点才直接读 containerSize。

相关页面