简体   繁体   English

如何在 Jetpack Compose 中更改行边框的形状?

[英]How to change the shape of a Row's border in Jetpack Compose?

Problem description问题描述

How to change the shape of a Row border in Jetpack Compose?如何在 Jetpack Compose 中更改Row边框的形状?

I'm creating a custom button in jetpack compose and for that I'm using a Row to align the content horizontally.我在 jetpack compose 中创建了一个自定义按钮,为此我使用Row来水平对齐内容。 I need this custom button to have the following format:我需要这个自定义按钮具有以下格式:

However my code in compose renders the following element, without the rounded edges:但是,我在 compose 中的代码呈现以下元素,没有圆边:

The code for this compose function is below:这个 compose function 的代码如下:

@Composable
fun LargeQuantityButton(
    modifier: Modifier = Modifier,
    onPlusClick: () -> Unit,
    onMinusClick: () -> Unit,
    text: String,
) = Row(
    modifier = modifier
        .border(
            border = ButtonDefaults.outlinedBorder,
            shape = RoundedCornerShape(4.dp)
        ),
    verticalAlignment = Alignment.CenterVertically,
    horizontalArrangement = Arrangement.SpaceBetween
) {
    RemoveIcon(action = onMinusClick)
    Text(
        text = text,
        fontWeight = FontWeight.W400
    )
    AddIcon(action = onPlusClick)
}

My attempt fails我的尝试失败了

I tried adding the clip function call right after the border call of the Modifier like this:我尝试在Modifier器的border调用之后添加clip function 调用,如下所示:

@Composable
fun LargeQuantityButton(
    modifier: Modifier = Modifier,
    onPlusClick: () -> Unit,
    onMinusClick: () -> Unit,
    text: String,
) = Row(
    modifier = modifier
        .border(border = ButtonDefaults.outlinedBorder)
        .clip(shape = RoundedCornerShape(4.dp)),
    verticalAlignment = Alignment.CenterVertically,
    horizontalArrangement = Arrangement.SpaceBetween
) {
    RemoveIcon(action = onMinusClick)
    Text(
        text = text,
        fontWeight = FontWeight.W400
    )
    AddIcon(action = onPlusClick)
}

But it didn't work either.但它也没有用。

I didn't understand exactly why but when I added a .padding after the Modifier.border statement the problem was solved:我不明白为什么,但是当我在Modifier.border语句后添加.padding时,问题就解决了:

@Composable
fun LargeQuantityButton(
    modifier: Modifier = Modifier,
    onPlusClick: () -> Unit,
    onMinusClick: () -> Unit,
    text: String,
) = Row(
    modifier = modifier
        .border(
            border = ButtonDefaults.outlinedBorder,
            shape = RoundedCornerShape(4.dp)
        )
        .padding(PaddingValues(horizontal = 8.dp)),
    verticalAlignment = Alignment.CenterVertically,
    horizontalArrangement = Arrangement.SpaceBetween
) {
    RemoveIcon(action = onMinusClick)
    Text(
        text = text,
        fontWeight = FontWeight.W500
    )
    AddIcon(action = onPlusClick)
}

Result:结果:

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

相关问题 如何在 Jetpack compose 中更改边框的颜色,而与它的宽度和形状无关? - How can I change border's color independent of it's width and shape in Jetpack compose? 如何更改 Jetpack Compose 中文本字段的边框颜色? - How to change border color of textfield in jetpack compose? Jetpack Compose - 边框上带有弧形的 CardView - Jetpack Compose - CardView with Arc Shape on border 如何仅在 Jetpack Compose 中更改特定边的边框宽度? - How to change border width of a particular side only in jetpack compose? 如何更改卡片的边框颜色,在 Jetpack Compose 中点击卡片? - How to change the border color of a card, on card click in Jetpack Compose? 如何更改 Android Jetpack Compose 中的 OutlineTextField 边框宽度? - How change OutlineTextField border width in Android Jetpack Compose? 当用户点击它时,边界半径不会根据形状改变 - Border radius is not changing based on shape when user click on it jetpack compose 如何在 Jetpack Compose 中有虚线边框? - How to have dashed border in Jetpack Compose? 如何仅在 Jetpack Compose 的底部添加边框 - how to add border on bottom only in jetpack compose 如何使用jetpack compose删除卡片视图的边框 - How to remove border of card view with jetpack compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM