Jetpack Compose 入门:Card

CardOutlinedCardElevatedCard 是 material3 组件,可以通过设置 shape = RoundedCornerShape() 完成聊天气泡效果的 Card。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@Composable
fun CardScreen(
modifier: Modifier = Modifier
) {
val text = """
床前明月光,疑是地上霜。
举头望明月,低头思故乡。
""".trimIndent()

Column(
modifier = modifier.fillMaxWidth()
) {
Card(
modifier = modifier
.fillMaxWidth()
.padding(16.dp)
) {
Column(
modifier = modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
AsyncImage(
model = "https://unsplash.com/photos/ZaU21K_4ZpA",
contentDescription = null,
modifier = modifier
.size(80.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary.copy(alpha = 0.5f)),
contentScale = ContentScale.Crop,
)

Column(
modifier = modifier
.fillMaxWidth()
.weight(1f),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(text = "8ug.icu")
Text(text = "2023.08.22", style = MaterialTheme.typography.labelSmall)
}
}

Text(text = text)

AsyncImage(
model = "https://unsplash.com/photos/a-white-and-brown-dog-walking-across-a-lush-green-field-TDOM2os-JYs",
contentDescription = null,
modifier = modifier
.size(120.dp)
.clip(RoundedCornerShape(16.dp))
.background(MaterialTheme.colorScheme.primary.copy(alpha = 0.5f)),
contentScale = ContentScale.Inside
)

Row(
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
IconButton(onClick = { /*TODO*/ }) {
Icon(imageVector = Icons.Default.ThumbUp, contentDescription = null)
}
IconButton(onClick = { /*TODO*/ }) {
Icon(imageVector = Icons.Default.Share, contentDescription = null)
}
}
}
}

OutlinedCard(
modifier = modifier
.fillMaxWidth()
.padding(16.dp),
) {
Text(
text = text,
modifier = modifier
.fillMaxWidth()
.padding(16.dp)
)
}

ElevatedCard(
modifier = modifier
.fillMaxWidth()
.padding(16.dp),
) {
Text(
text = text,
modifier = modifier
.fillMaxWidth()
.padding(16.dp)
)
}

Card(
modifier = modifier
.fillMaxWidth()
.padding(16.dp),
shape = RoundedCornerShape(
topStart = 0.dp,
topEnd = 8.dp,
bottomStart = 8.dp,
bottomEnd = 8.dp
)
) {
Text(
text = text,
modifier = modifier
.fillMaxWidth()
.padding(16.dp)
)
}
}
}

Demo:https://github.com/hefengbao/jetpack-compose-demo