【问题标题】:Jetpack Compose Preview not showingJetpack Compose 预览未显示
【发布时间】:2021-04-26 11:55:52
【问题描述】:

我似乎在撰写时遇到了预览问题,当我使用 @preview 注释撰写方法时,布局面板没有出现。我假设我缺少一个依赖项,但我已经从这里 https://developer.android.com/jetpack/compose/setup 复制并粘贴了代码。有什么建议? (尝试了通常的清除缓存,重新打开项目等):)

buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.0.0-alpha10'
        kotlinCompilerVersion '1.4.21'
    }
}

dependencies {
    implementation 'androidx.compose.ui:ui:1.0.0-alpha10'
    // Tooling support (Previews, etc.)
    implementation 'androidx.compose.ui:ui-tooling:1.0.0-alpha10'
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation 'androidx.compose.foundation:foundation:1.0.0-alpha10'
    // Material Design
    implementation 'androidx.compose.material:material:1.0.0-alpha10'
    // Material design icons
    implementation 'androidx.compose.material:material-icons-core:1.0.0-alpha10'
    implementation 'androidx.compose.material:material-icons-extended:1.0.0-alpha10'
    // Integration with observables
    implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-alpha10'
    implementation 'androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha10'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.0-alpha10'

    implementation 'com.google.android.material:material:1.2.1'
}

这是我使用预览的尝试(在 AS 中,它说函数“DefaultPreview”从未使用过)

import androidx.compose.ui.tooling.preview.Preview
.....
@Preview
@Composable
fun DefaultPreview() {
    Text(text = "Hello!")
}

【问题讨论】:

    标签: android android-jetpack-compose


    【解决方案1】:

    如果其他人遇到与我相同的问题(这是用户错误,但我也认为文档可能更清晰),我将保留此内容。

    Android Canary 有两个版本,测试版和北极狐 (alpha)。如果您想使用最新版本的 compose 库,请确保您使用的是 arctic fox。我发现 compose 库 'androidx.compose.ui:ui-tooling:1.0.0-alpha08'(及更高版本)不适用于 canary 测试版。

    【讨论】:

      【解决方案2】:
          buildFeatures {
              compose true
          }
      

      在 build.gradle 文件的 Android 块内编写上述代码。 你的问题就会得到解决。

      【讨论】:

      • 这是我在项目中缺少的。 Compose 文件右上角的选项(代码/拆分/设计)在我添加之前没有显示。
      【解决方案3】:

      我想你可以在配置中找到你想要的东西

      【讨论】:

        【解决方案4】:

        Jetpack Compose(rc01、rc02)与@Preview 存在问题。您可以通过在 build.gradle 文件中添加以下代码来解决它:

        android {
           ...
        }
        
        configurations.all {
            resolutionStrategy {
                force("androidx.compose.ui:ui-tooling:1.0.0-beta09")
                force("androidx.compose.ui:ui-tooling-data:1.0.0-beta09")
                force("androidx.compose.ui:ui-tooling-preview:1.0.0-beta09")
            }
        }
        
        dependencies {
           ...
        }
        

        示例:https://github.com/AlexZhukovich/ComposePlayground/blob/main/app/build.gradle

        【讨论】:

          【解决方案5】:

          更新到最新版本的 AS 解决了我的错误。

          从这里https://developer.android.com/studio/preview尝试最新版本

          【讨论】:

          • 谢谢大佬,我用的是Android studio Bumblebee版本,问题解决了。
          【解决方案6】:

          对我来说,我的 gradle 文件中没有以下内容:

          composeOptions {
              kotlinCompilerExtensionVersion '1.0.3'
          }
          

          buildFeatures {
             compose true
          }
          
          

          【讨论】:

            【解决方案7】:

            在项目的 build.gradle 中指定:

            dependencies {
                        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
                    }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-08-04
              • 2021-10-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-08-28
              • 2021-08-05
              • 2022-07-20
              相关资源
              最近更新 更多