Ral*_*kum 2 android android-jetpack android-jetpack-compose
图片 我在 Android 中有一个普通按钮和一个星形图标。我想将它们组合成一个新的按钮图标,其中星星位于右上角之一,如下所示:
当我使用时,Row
两者是分开的。正如您所看到的,星形应与按钮的一角重叠。我怎样才能做到这一点?
编辑:感谢加布里埃尔·马里奥蒂,我用了
Box {
Button(
id = "btnButton",
modifier = Modifier
.padding(end = 48)
onClick = {
//..
}
)
IconWithStar(
modifier = Modifier
.scale(0.65f)
)
}
Run Code Online (Sandbox Code Playgroud)
左上角绑定了星形图标,我该如何修改?
您可以使用 a 包裹可组合项Box
,并使用align
/offset
修饰符来调整它们的位置。
Box(Modifier.padding(top=40.dp)){
Button(
onClick = {})
{
Text("Hello World")
}
Icon(
Icons.Filled.Star, "",
modifier =Modifier
.align(TopEnd)
.offset(12.dp,-12.dp),
tint = Yellow600
)
}
Run Code Online (Sandbox Code Playgroud)
要获得更多控制,您可以构建自定义Layout
.
就像是:
Layout( content = {
Button(
modifier = Modifier.layoutId("button"),
onClick = { /* ... */ })
{
Text("Hello World")
}
Icon(Icons.Filled.Star, "",
Modifier.layoutId("icon"),
tint = Yellow600)
}){ measurables, incomingConstraints ->
val constraints = incomingConstraints.copy(minWidth = 0, minHeight = 0)
val buttonPlaceable =
measurables.find { it.layoutId == "button" }?.measure(constraints)
val iconPlaceable =
measurables.find { it.layoutId == "icon" }?.measure(constraints)
//align the icon on the top/end edge
layout(width = widthOrZero(buttonPlaceable) + widthOrZero(iconPlaceable)/2,
height = heightOrZero(buttonPlaceable)+ heightOrZero(iconPlaceable)/2){
buttonPlaceable?.placeRelative(0, heightOrZero(iconPlaceable)/2)
iconPlaceable?.placeRelative(widthOrZero(buttonPlaceable)- widthOrZero(iconPlaceable)/2,
0)
}
}
internal fun widthOrZero(placeable: Placeable?) = placeable?.width ?: 0
internal fun heightOrZero(placeable: Placeable?) = placeable?.height ?: 0
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1132 次 |
最近记录: |