遇到的问题
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!")
// }
}