最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show Android device frame in Android Studio Jetpack Compose Preview - Stack Overflow

programmeradmin3浏览0评论

I am using the following code:

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

And I see this. I cannot see the preview of Android device. I mean I want to see the frame of the device but I don't see it. What am I missing?

How can I add a device frame?

I am using the following code:

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

And I see this. I cannot see the preview of Android device. I mean I want to see the frame of the device but I don't see it. What am I missing?

How can I add a device frame?

Share Improve this question asked Jan 20 at 5:40 john doejohn doe 9,66023 gold badges94 silver badges176 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

If you want the full phyiscal device frame to show up, that is not possible currently.

The closest you can achieve is by using showSystemUi together with a device parameter, like this:

@Preview(showSystemUi = true, device = Devices.PIXEL_4A)
@Composable
fun Greeting(name: String = "you", modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier.safeDrawingPadding()
    )
}

You will then get a Preview that

  • matches the dimensions of the selected device
  • offers the cutouts / camera punch hole of the selected device
  • shows the status bar and navigation pill.

Output:

You need to add something like this:

@Preview(showBackground = true)
@Composable
fun Greeting(name: String = "there", modifier: Modifier = Modifier) {
    //The screen will expand to a phones bound
    Box(Modifier.fillMaxSize()) {
        Text(
            text = "Hello $name!",
            modifier = modifier
        )
    }
}

Add @Preview(showSystemUi = true)

@Preview(showSystemUi = true)
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
    text = "Hello $name!",
    modifier = modifier
)
}

For More type of preview refer Preview your UI with composable previews

Use the Device Manager in Android Studio to find device IDs or reference the official documentation for available device IDs. Common options include:

  • id: pixel_6_pro
  • id: pixel_4
  • id: pixel_3a
  • id: tablet
@Preview(
    name = "Pixel 6 Pro Preview",
    showBackground = true,
    showSystemUi = true,
    device = "id:pixel_6_pro"
)
@Composable
fun MyComposablePreview() {
    MyComposable()
}
发布评论

评论列表(0)

  1. 暂无评论