我想使用 显示项目列表LazyColumn
。我编写了所需的代码,但当我运行该应用程序时,我发现 UI 非常滞后。我放了一些Logs
来找出问题可能出在哪里,我发现LazyColumn
无限地重新组合这些项目。我将 替换为LazyColumn
withColumn
可滚动修饰符,在这种情况下问题就消失了。但我不知道为什么LazyColumn
会有这样的表现。
名单是这样的:
var rides = mutableStateListOf<Ride>(Ride(...), Ride(...))
Run Code Online (Sandbox Code Playgroud)
一个列表项是这样的:
@Stable
@Entity(tableName = "ride_table")
data class Ride(
var img: Bitmap? = null,
var path: List<List<LatLng>>? = null,
var cities: List<String>? = null,
var timeStarted: Long = 0L,
var timeEnded: Long = 0L,
var avgSpeedInKmh: Float = 0f,
var maxSpeedInKmh: Float = 0f,
var distanceInMeters: Float = 0f,
var totalTimeInSeconds: Long = 0L,
@PrimaryKey(autoGenerate = true)
var …
Run Code Online (Sandbox Code Playgroud) 对于android.widget.Button
我可以用来performClick()
以编程方式模拟点击。但我不知道如何在 Jetpack Compose 中做到这一点。我查看了compose.material
文档,但至少我找不到任何关于此的信息。
我想要为我的LazyColumn
. 我成功地使按钮起作用了。但如果我已经位于 的顶部,我希望它不可见LazyColumn
。我怎样才能做到这一点?
考虑下图:
我有一个Column
与另一个有一些元素Image
的一起。我想做的是将比例统一到可用空间。我就是无法让它发挥作用。Column
Text
Image
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.Cyan)
.padding(horizontal = 16.dp),
verticalArrangement = Arrangement.SpaceEvenly
) {
Image(
modifier = Modifier.fillMaxWidth(),
painter = painterResource(R.drawable.rocket_boy),
contentDescription = null,
contentScale = ContentScale.Fit
)
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
repeat(20) {
Text(text = "${it}")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试的其中一件事是将Image
tofillMaxSize
和weight
第二个Column
to的大小设置为1f
,但它没有做任何事情。也许我需要Column
固定第二个的大小?非常感谢任何帮助。
我正在构建一个自定义芯片Jetpack Compose
,我想获得以下外观(关于颜色):
那些灰色是什么颜色?我在material.io 上都见过它们。我假设第一个芯片是通过在“原始”芯片上应用一些 alpha 修改器获得的。如果是这样,我该怎么做?实现这种禁用外观的常规 alpha 值是多少?
I am trying to center the content inside a Text
of Jetpack Compose, but I'm not succeeding. The code seems fine to me (I'm very new to Compose though). The code:
Row(
modifier = Modifier
.padding(8.dp)
.height(30.dp)
.fillMaxHeight(),
) {
IconButton(..) {..}
Spacer(modifier = Modifier.width(4.dp))
Text(
text = "Some text",
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxHeight()
.border(1.dp, Color.Black, RoundedCornerShape(20))
.width(120.dp)
.background(color = Color.White, RoundedCornerShape(20))
.align(Alignment.CenterVertically)
)
Spacer(modifier = Modifier.width(4.dp))
IconButton(..) {..}
}
Run Code Online (Sandbox Code Playgroud)
I am trying to make a custom TopBar for my application and I want to have a DropdownMenu
displayed in the top right corner of the screen. I have a Box
that contains a Row
(with some text and icons) and a DropdownMenu
which is initially not displayed. When I click on an icon, the DropdownMenu
is displayed, but outside the Box
, so not where I intended. The code:
@Composable
private fun TopBar {
var expanded by remember { …
Run Code Online (Sandbox Code Playgroud) 我想获得一个LazyRow
看起来像这样的:
|--aaa-b|bb-cccc|-dd... ...|w--x---|
|--------| 是一屏宽度
元素的大小各不相同,但它们之间的间距是固定的。我想我可以添加一些开始内容填充,以便LazyRow
“aaa”可组合项与屏幕中心对齐,但我不知道它的宽度。
如果您认为我问的不清楚,请发表评论。
更新
添加了 gif 以便更好地理解
android android-jetpack-compose android-jetpack-compose-list
我想显示 aText
内部Card
带有一些内部填充,有时文本不适合。我希望这个东西用省略号标记。但如果没有maxLines
.
@Composable
fun CardWithText() {
Card(
modifier = Modifier
.height(60.dp)
.width(100.dp)
.border(1.dp, Color.Black, RoundedCornerShape(0))
) {
Card(
modifier = Modifier
.padding(8.dp)
.fillMaxSize()
.border(1.dp, Color.Black, RoundedCornerShape(0))
) {
Text(
text = "One two three four five six seven eight nine ten eleven twelve",
maxLines = 2,
overflow = TextOverflow.Ellipsis,
color = Color.Black
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
和maxLines = 2
使用maxLines = 3
或maxLines
根本不使用
kotlin android-jetpack android-jetpack-compose android-jetpack-compose-text