簡體   English   中英

如何僅在 Jetpack Compose 中更改特定邊的邊框寬度?

[英]How to change border width of a particular side only in jetpack compose?

我正在嘗試實現以下布局

在此處輸入圖像描述

無法在 Jetpack Compose 中設置單獨的邊框寬度

這是我的代碼

            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)
                )
            }

我使用了不同的 colors,但你可以看到結果:

我使用了不同的顏色,但你可以看到結果

我看到了三種方法,如何實現這種效果:

  1. 您可以創建自定義修改器擴展方法,就像我在我的一個項目中所做的那樣,使用 drawBehind 修改器:
@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)
        )
    }
}

這可以使用帶有背景的 Box 作為邊框並添加 padding more padding to start than other corners 來完成

在此處輸入圖像描述

@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)
            )
        }
    }
}
Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(stops: const [
          0.014,
          0.014
        ], colors: [
          isError ? redColor : greenColor,
          Theme.of(context).colorScheme.background
        ]),
        color: Theme.of(context).colorScheme.background,
        borderRadius: BorderRadius.circular(10),
        border: Border.all(color: isError ? redColor : greenColor, width: 1),
      ),
      padding: const EdgeInsets.symmetric(
        vertical: 12,
        horizontal: 12,
      ),
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: Text(
              text,
              style: GoogleFonts.inter(
                fontWeight: FontWeight.w500,
                fontSize: 12,
                color: gray6Color,
              ),
            ),
          )
        ],
      )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM