Azu*_*123 4 android kotlin android-jetpack-compose
我有一个可组合函数,它为表面分配大小。
@Composable
private fun CreateImage(modifier: Modifier = Modifier) {
Surface(
modifier = modifier
.size(150.dp)
.padding(5.dp),
shape = CircleShape,
border = BorderStroke(1.dp, Color.LightGray)
) {
Image(
painter = painterResource(id = R.drawable.profile_image),
contentDescription = "Profile Image",
contentScale = ContentScale.Crop
)
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用另一个方法并更改修饰符参数中的大小时,它不应该坚持 150dp。
如果我调用这个方法:
@Composable
private fun ChangeSize(name: String) {
CreateImage(Modifier.size(100.dp))
}
Run Code Online (Sandbox Code Playgroud)
即使在 CreateImage 中我将其设置为 150dp,它的大小仍保持为 100dp。为什么尺寸没有变为 150dp 而是保持 100dp?
我认为应该将其更改为 150dp。为什么事实并非如此?
修改器使用它获得的第一个尺寸,当人们不向您的可组合项提供任何尺寸时,这是一个很棒的功能。
例如
CircleDisplay(
modifier = circleModifier
.widthIn(min = 70.dp)
.heightIn(min = 70.dp),
color = color
)
Run Code Online (Sandbox Code Playgroud)
如果有人没有提供任何宽度或高度的修饰符,而不是提供 0 高度和宽度,则您可以为可组合项指定最小尺寸。您可以根据您的实现将其更改为最大或精确大小。但是,当用户修饰符具有一些宽度高度而不是您的宽度高度时,由于使用第一个尺寸,因此将使用他们提供的宽度高度。
像 Slider 这样的默认可组合项也使用此模式,因此在不设置任何尺寸的情况下,它的高度为 48.dp,并填充其父级的最大宽度。
Slider 下的 BoxWithConstraint 如下
BoxWithConstraints(
modifier
.minimumTouchTargetSize()
.requiredSizeIn(minWidth = ThumbRadius * 2, minHeight = ThumbRadius * 2)
.sliderSemantics(value, tickFractions, enabled, onValueChange, valueRange, steps)
.focusable(enabled, interactionSource)
) {
// Rest of the Slider Implementation
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
790 次 |
| 最近记录: |