瑶吖瑶吖瑶 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日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因