瑶吖瑶吖瑶 2023-02-01 17:05 采纳率: 100%
浏览 24
已结题

Android使用compose时预览窗口不显示

遇到的问题

Android使用compose时预览窗口无法显示?

问题信息

首先排除版本不对,我的 compose是1.3.2 --> kotlin是1.7.20
排除依赖没有添加,改加的都加了

composeOptions { kotlinCompilerExtensionVersion = "1.3.2" }

    buildFeatures{
        dataBinding = true
        compose = true
    }

dependencies {
    // 借助 Compose 物料清单 (BoM),您只需指定 BoM 的版本,即可管理所有 Compose 库版本。
    // BoM 本身包含指向不同 Compose 库的稳定版的链接,以便它们能够很好地协同工作。
    // 在应用中使用 BoM 时,您无需向 Compose 库依赖项本身添加任何版本。
    // 更新 BoM 版本时,您使用的所有库都会自动更新到新版本。

    def composeBom = platform('androidx.compose:compose-bom:2022.12.00')
    implementation composeBom
    androidTestImplementation composeBom

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    // Material Design 3
    implementation 'androidx.compose.material3:material3'
    // 输入和测量/布局
    implementation 'androidx.compose.ui:ui'

    // Android Studio Preview support
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Optional - 按材料自动包含, 仅在需要时添加图标而不是材质库(
    // 例如,使用Material3或基于Foundation的自定义设计系统)
    implementation 'androidx.compose.material:material-icons-core'
    // Optional - 添加全套材质图标
    implementation 'androidx.compose.material:material-icons-extended'
    // Optional - 添加窗口大小实用程序
    implementation 'androidx.compose.material3:material3-window-size-class'

    // Optional - 与活动集成
    implementation 'androidx.activity:activity-compose:1.5.1'
    // Optional - Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
    // Optional - Integration with LiveData
    implementation 'androidx.compose.runtime:runtime-livedata'
    // Optional - Integration with RxJava
    implementation 'androidx.compose.runtime:runtime-rxjava2'
}
我的代码区

package com.wy.myjetpacktest.card

import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.input.key.Key.Companion.H
import androidx.compose.ui.input.key.Key.Companion.I
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.wy.myjetpacktest.R


/**
 * @author 
 * @date 2023/1/31
 * @description
 */
class BirthdayActivity: ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { 
            Greeting( "安倍的碟")
//            HappyBirthdayTheme {
//
//            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    // Row 横向
    Row{
        Color(0xFFFC6F65)
        Text(text = "Hello $name")
        Text(text = "jhbsjhb")
        Image(ImageBitmap.imageResource(id = R.mipmap.ic_launcher), contentDescription = null,
        modifier = Modifier
            .width(300.dp)
            .height(400.dp)
            .padding(10.dp)
            // 设置圆角
            .clip(shape = RoundedCornerShape(20.dp)))
    }
    // 纵向
    Column(content = {
        Text(text = "modjj")
        Text(text = "hdchuh")
    })

}

@Composable
fun BirthdayGreetingWithText(message: String, from: String){
    Column {
        Text(
            text = message,
            // 更改字体大小
            fontSize = 36.sp
        )

        Text(
            text = from,
            fontSize = 24.sp,

        )
    }
}

@Preview
@Composable
fun BirthdayCardPreview() {
//    HappyBirthdayTheme {
//        BirthdayGreetingWithText(message = "Happy Birthday Amy!")
//    }
}


运行结果及详细报错内容:没有任何报错,仅仅只是预览窗口不显示
我更换过kotlin版本,但是行不通
  • 写回答

2条回答 默认 最新

  • 瑶吖瑶吖瑶 2023-02-01 17:55
    关注

    破案了,创建项目的时选择的风格不对,应该选MD3(Empty Compose Activity(material3))这一个,而我之间是直接在以前的项目里新建的kotlin文件,然后往这个文件里添加compose需要的环境和依赖(囧)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月9日
  • 已采纳回答 2月1日
  • 创建了问题 2月1日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”