简体   繁体   English

Jetpack Compose 实时预览

[英]Jetpack compose live preview

I just started working on jetpack compose and downloaded sample of Jetnews .我刚刚开始研究 Jetpack Compose 并下载了Jetnews示例。

When I opened MainActivity or JetnewsApp.kt I am unable to see preview of Composable function, I added @Preview annotation as well but unable to see live preview.当我打开MainActivityJetnewsApp.kt时,我无法看到 Composable function 的预览,我也添加了@Preview注释,但无法看到实时预览。

Can anyone please help me to find live preview.谁能帮我找到实时预览。
Thanks in advance.提前致谢。

If your Preview never show after add @Preview annotation in JetnewsApp class.如果在JetnewsApp class 中添加@Preview注释后您的预览从未显示。 In my case after adding @Preview i close project and then open project again it's working fine for me in Macbook pro.在我的情况下,添加@Preview后我关闭项目,然后再次打开项目,它在 Macbook pro 中对我来说工作正常。

In JetnewsApp , SelectTopicButton.kt only have @Preview() tags.JetnewsApp中, SelectTopicButton.kt只有@Preview()标签。 When in any file we have @Preview() it will automatically show preview in the right side.当在任何文件中我们有@Preview()时,它会自动在右侧显示预览。

In show decoration mode you can get the preview of the item with the mobile image, how does it look on the screen.显示装饰模式下,您可以使用移动图像预览项目,它在屏幕上的外观。

To get a live preview we need to add @Preview tag before @Composable tag and the fun must have something to render.要获得实时预览,我们需要在@Composable标记之前添加@Preview标记,并且必须要渲染一些有趣的东西。


I had tried adding @Preview() and @Preview("MyScreen preview") in JetnewsApp.kt and it shows the preview.我曾尝试在 JetnewsApp.kt 中添加@Preview()@Preview("MyScreen preview")并显示预览。

在此处输入图像描述

Update:更新:

Before that we have to add @Preview on JetnewsApp to get preview.在此之前,我们必须在@Preview JetnewsApp获得预览。

@Preview
@Composable
fun JetnewsApp() { ... }

You just need to check the "Show Decorations" option to show real layout preview.您只需要选中“显示装饰”选项即可显示真实的布局预览。

You can toggle both preview using same option.您可以使用相同的选项切换两个预览。

Check the below image:检查下图:

在此处输入图像描述

Update 2:更新 2:

Also invalidate and restart Android Studio if it doesn't work at first.如果 Android Studio 一开始不起作用,也将其无效并重新启动。 Remember this is still in development.请记住,这仍在开发中。

btw currently @Preview just work for @Composable function without params.顺便说一句,目前@Preview仅适用于@Composable function没有参数。 so please check again your composable function所以请再次检查您的可组合 function

I had a similar problem where the preview wouldn't show.我有一个类似的问题,预览不会显示。 The problem was that I had a call to setContent in the function also.问题是我也调用了 function 中的setContent Removing this and rebuilding rendered the preview as expected.删除它并重建按预期呈现预览。

So所以

    @Preview(showBackground = true, widthDp = 320)
    @Composable
    fun PreviewMainScreen() {
        setContent {
            Greeting()
        }
    }

would fail and会失败并且

    @Preview(showBackground = true, widthDp = 320)
    @Composable
    fun PreviewMainScreen() {
        Greeting()
    }

worked as expected.按预期工作。

Surprisingly, and this is the bit I don't understand, once the preview had rendered correctly, I could add the setContent again and it would update and still show.令人惊讶的是,这是我不明白的一点,一旦预览正确呈现,我可以再次添加setContent ,它会更新并仍然显示。

I hope the answers in the previous post helped.我希望上一篇文章中的答案有所帮助。 However, I want to add a simple feature that will assist while minor modifications are made to the Jetpack compose code, preventing the need to frequently launch your app in order to view the altered layout.但是,我想添加一个简单的功能,在对 Jetpack 组合代码进行微小修改时提供帮助,避免需要频繁启动您的应用程序以查看更改后的布局。

Steps脚步

  1. Live edit of literals should be ON.文字的实时编辑应该是开的。 It will be placed to the left side of Code|Split|Design.它将放置在 Code|Split|Design 的左侧。

  2. Start interactive mode.启动交互模式。 It will be placed to the page of function preview.会放到function预览页面。 below Code|Split|Design.在代码|拆分|设计下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM