Modifier的操作符都是扩展函数,并不是定义在一起。操作符定义在不同的空间中,可以限制某些操作符只能在特定父Comopsable中使用,避免误用。
interface RowScope {
fun Modifier.align(alignment: Alignment.Vertical)
}
复制代码
如上,align
只能在Row中调用,用来设置子元素在垂直方向如何对齐。子元素不关心其在父容器中如何对齐,因此在外部设置align(Alignment.CenterVertically)
后,传给子元素继续使用。
Modifier.weight
weight
同样只能在Row中调用,为子元素分配在Row中的占比,类似于LinearLayout
的layout_weight
。本例中让中间的文字部分占据所有所有空间
我们对头像图片做圆形处理并添加边框,提升整体视觉效果。
@Composable
fun Avatar(modifier: Modifier) {
Image(
modifier = modifier
.size(50.dp)
.clip(CircleShape)
.border(
shape = CircleShape,
border = BorderStroke(
width = 2.dp,
brush = Brush.linearGradient(
colors = listOf(blue, teal200, green200, orange),
start = Offset( 0f, 0f),
end = Offset(100f,100f)
)
)
)
.border(
shape = CircleShape,
border = BorderSt