简体   繁体   English

Android Jetpack 在 Box 中组合部分边框

[英]Android Jetpack compose partial border in Box

I am trying to prepare a design in jetpack compose with a partial border on one side of the box.我正在尝试在喷气背包中准备一个设计,在盒子的一侧有一个部分边框。 Here is the UI I have right now,这是我现在拥有的用户界面,

The background is a solid color as of now but will be replaced with a image.目前背景是纯色,但将替换为图像。 I want to break border on bottom left of and add some text similar to the screenshot below while keeping the background as it is.我想打破左下角的边框并添加一些类似于下面的屏幕截图的文本,同时保持背景原样。

Here is my code:这是我的代码:

Box(modifier = Modifier
    .fillMaxWidth()
    .fillMaxHeight()) {
    Box(modifier = Modifier.fillMaxHeight().fillMaxWidth().background(Color(0xFF37C7D7).copy(alpha = 0.6f)))
    Box(modifier = Modifier
        .fillMaxHeight()
        .fillMaxWidth()
        .background(Color.Transparent)
        .padding(20.dp,30.dp)
        .border(width = 0.8.dp, color = Color.White.copy(alpha = 0.5f), shape=RoundedCornerShape(32.dp))
    )
    Box(modifier = Modifier
        .fillMaxHeight()
        .fillMaxWidth()
        .background(Color.Transparent)
        .padding(28.dp,38.dp)
        .border(width = 0.8.dp, color = Color.White.copy(alpha = 0.5f), shape=RoundedCornerShape(28.dp))
    )
    Column(modifier = Modifier.statusBarsPadding()
        .fillMaxWidth()
        .fillMaxHeight().padding(20.dp,40.dp),
        verticalArrangement = Arrangement.Bottom) {
        Text(text = "this is Test",modifier = Modifier.padding(0.dp,30.dp))
    }
}

You could create a custom Shape , but then you have to pass the location of the text to that shape, which I think is over-engineering(unless you have a dynamic background such as an image).您可以创建自定义Shape ,但随后您必须将文本的位置传递给该形状,我认为这是过度设计的(除非您具有动态背景,例如图像)。

Instead, you can add background color to your Text , which will much the parent background.相反,您可以将背景颜色添加到您的Text ,这将是父背景。

If your background view is just an other color, you can use ColorUtils.blendARGB to calculate output color, like this:如果您的背景视图只是其他颜色,您可以使用ColorUtils.blendARGB来计算输出颜色,如下所示:

val parentBackground = Color.White
val cardBackground = Color(0xFF37C7D7)
val cardBackgroundAlpha = 0.6f
Box(modifier = Modifier
    .fillMaxWidth()
    .fillMaxHeight()
    .background(parentBackground)
) {
    Box(modifier = Modifier.fillMaxHeight().fillMaxWidth().background(cardBackground.copy(alpha = cardBackgroundAlpha)))
    Box(modifier = Modifier
        .fillMaxHeight()
        .fillMaxWidth()
        .background(Color.Transparent)
        .padding(20.dp,30.dp)
        .border(width = 0.8.dp, color = Color.White.copy(alpha = 0.5f), shape=RoundedCornerShape(32.dp))
    )
    Box(modifier = Modifier
        .fillMaxHeight()
        .fillMaxWidth()
        .background(Color.Transparent)
        .padding(28.dp,38.dp)
        .border(width = 0.8.dp, color = Color.White.copy(alpha = 0.5f), shape=RoundedCornerShape(28.dp))
    )
    Column(
        verticalArrangement = Arrangement.Bottom,
        modifier = Modifier.statusBarsPadding()
            .padding(20.dp,40.dp)
            .background(Color(ColorUtils.blendARGB(parentBackground.toArgb(), cardBackground.toArgb(), cardBackgroundAlpha)))
            .align(Alignment.BottomStart)
    ) {
        Text(text = "this is Test",modifier = Modifier.padding(0.dp,30.dp))
    }
}

Result:结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM