如何知道 Android 系统是平板电脑还是手机

sof*_* cr 4 kotlin android-jetpack-compose

我在 Swift 中有一个这样的 var :

@Published  var textFieldSize = UIDevice.current.userInterfaceIdiom == .pad ? 20.0 :  12.0
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 Jetpack Compose Android 中获得相同的设备类型(手机或平板电脑)?

Quố*_*ùng 8

根据 \xd7\x90\xd7\xa8\xd7\x99\xd7\x90\xd7\x9c \xd7\xa2\xd7\x93\xd7\x9f 和 Code Poet 的答案,您可以使用此函数:

\n
@Composable\nfun isTablet(): Boolean {\n    val configuration = LocalConfiguration.current\n    return if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {\n        configuration.screenWidthDp > 840\n    } else {\n        configuration.screenWidthDp > 600\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

并在您的可组合项中调用它,例如:

\n
@Composable\nfun MainScreen() {\n    val isTablet = isTablet()\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n