android 如何改变边界宽度的一个特定的一方只在喷气背包组成?

bfnvny8b  于 2023-01-24  发布在  Android
关注(0)|答案(2)|浏览(99)

我正在尝试实现以下布局
enter image description here
在jetpack合成中无法设置单独的边框宽度
这是我的密码

Row(
                modifier = Modifier
                    .border(width = 1.dp, shape = RoundedCornerShape(4.dp), color = Purple3)
                    .padding(10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.info),
                    contentDescription = stringResource(id = R.string.long_press_an_item),
                )
                Text(
                    text = stringResource(id = R.string.long_press_an_item),
                    fontFamily = FontFamily(Font(R.font.inter_medium)),
                    fontSize = 12.sp,
                    color = Gray5,
                    modifier = Modifier.padding(horizontal = 10.dp)
                )
            }
vatpfxk5

vatpfxk51#

我用了不同的颜色,但你可以看到结果:

我看到有三种方法,你可以达到这个效果:
1.您可以创建自定义修饰符扩展方法,就像我在我的一个项目中所做的那样,使用drawBehind Modifier:

@Preview
@Composable
fun Preview() {
    Row(
        modifier = Modifier
            .someCustomOutline(
                outlineColor = MaterialTheme.colors.primary,
                surfaceColor = MaterialTheme.colors.surface,
                startOffset = 3.dp,
                outlineWidth = 1.dp,
                radius = 4.dp
            )
            .padding(10.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            imageVector = Icons.Default.Info,
            contentDescription = null,
        )
        Text(
            text = "long_press_an_item",
            fontSize = 12.sp,
            modifier = Modifier.padding(horizontal = 10.dp)
        )
    }
}

@Composable
fun Modifier.someCustomOutline(
    outlineColor: Color,
    surfaceColor: Color,
    startOffset: Dp,
    outlineWidth: Dp,
    radius: Dp = 1.dp
) = drawBehind {
    val startOffsetPx = startOffset.toPx()
    val outlineWidthPx = outlineWidth.toPx()
    val radiusPx = radius.toPx()
    drawRoundRect(
        color = outlineColor,
        topLeft = Offset(0f, 0f),
        size = size,
        cornerRadius = CornerRadius(radiusPx, radiusPx),
        style = Fill
    )
    drawRoundRect(
        color = surfaceColor,
        topLeft = Offset(startOffsetPx, outlineWidthPx),
        size = Size(size.width - startOffsetPx - outlineWidthPx, size.height - outlineWidthPx * 2),
        cornerRadius = CornerRadius(radiusPx - outlineWidthPx, radiusPx - outlineWidthPx),
        style = Fill
    )
}

用法:

这是一个非常糟糕的方法,但我还是要提到它以防万一
1.你可以用Box来压缩你的元素,然后给它添加填充:

Box(
    modifier = Modifier
        .background(shape = RoundedCornerShape(4.dp), color = Purple3)
        .padding(start = 3.dp, top = 1.dp, end = 1.dp, bottom = 1.dp)
) {
    Row(
        modifier = Modifier
            .background(MaterialTheme.colors.surface, shape = RoundedCornerShape(3.dp))
            .padding(10.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            imageVector = painterResource(id = R.drawable.info),
            contentDescription = stringResource(id = R.string.long_press_an_item),
        )
        Text(
            text = stringResource(id = R.string.long_press_an_item),
            fontSize = 12.sp,
            fontFamily = FontFamily(Font(R.font.inter_medium)),
            color = Gray5,
            modifier = Modifier.padding(horizontal = 10.dp)
        )
    }
}
vxqlmq5t

vxqlmq5t2#

这可以使用一个有背景的框作为你的边界,并添加填充比其他角更多的填充开始

@Preview
@Composable
private fun Test() {
    Box(modifier = Modifier.background(Color(0xffBA68C8), RoundedCornerShape(4.dp))) {
        Row(
            modifier = Modifier
                .padding(start = 4.dp, top = 1.dp, bottom = 1.dp, end = 1.dp)
                .background(Color.White, RoundedCornerShape(4.dp))
                .padding(10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                imageVector = Icons.Default.Info,
                contentDescription = "Long press item to enable multi-select and bulk operations"
            )
            Text(
                text = "Long Press on Item",
                fontSize = 12.sp,
                color = Color.Gray,
                modifier = Modifier.padding(horizontal = 10.dp)
            )
        }
    }
}

相关问题