简体   繁体   English

如何在 Jetpack Compose 中删除 RadioButton 的填充?

[英]How to remove RadioButton's padding in Jetpack Compose?

I have a problem with RadioButton component in my Jetpack Compose application.我的 Jetpack Compose 应用程序中的 RadioButton 组件有问题。 I have some RadioButtons with text and this have a lot of padding by default.我有一些带文本的 RadioButtons,默认情况下有很多填充。 Can I remove this padding or to set a custom padding to avoid a lot of space between each?我可以删除此填充或设置自定义填充以避免每个填充之间有很多空间吗?

Currently I have this:目前我有这个:

在此处输入图像描述

My code is:我的代码是:

Column {
    MyEnum.values().filter { rb -> rb.visible }.forEach { rb ->
        Row(
            Modifier
                .fillMaxWidth()
                .padding(horizontal = 0.dp, vertical = 0.dp)
                .clickable(
                    interactionSource = interactionSource,
                    indication = null
                ) {
                    TODO()
                },
            verticalAlignment = Alignment.CenterVertically
        ) {
            RadioButton(
                selected = (rb.position == selectedOption),
                onClick = {
                    TODO()
                },
                colors = RadioButtonDefaults.colors(
                    selectedColor = DialogOutlinedTextFocus,
                    unselectedColor = DialogOutlinedTextUnfocus
                )
            )
    
            Text(
                text = stringResource(id = rb.idText),
                color = Color.Black,
                fontSize = 14.sp,
                modifier = Modifier
                    .padding(horizontal = 3.dp, vertical = 2.dp)
            )
        }
    }
}

I tried with contentPadding, but this property does not exist in RadioButton component.我尝试使用 contentPadding,但 RadioButton 组件中不存在此属性。

The source code for RadioButton.kt sets the padding modifier at line 108. With modifiers, order matters. RadioButton.kt的源代码在第 108 行设置了填充修饰符。有了修饰符,顺序很重要。 Even if you provide your own padding modifier it will be overridden by line 108.即使您提供自己的填充修饰符,它也会被第 108 行覆盖。

单选按钮.kt

The sizing values are hardcoded at the bottom of the file.尺寸值硬编码在文件底部。

单选按钮.kt

If you really want to "reduce the padding", apply a size modifier.如果您真的想“减少填充”,请应用size修饰符。 I use 20.dp because it's equal to radioButtonSize in RadioButton.kt so the button doesn't get clipped.我使用20.dp ,因为它等于RadioButton.kt中的radioButtonSize ,因此按钮不会被剪裁。 This should work for your purposes since the entire row is clickable.这应该适合您的目的,因为整行都是可点击的。

RadioButton(
    modifier = Modifier.size(20.dp),
    selected = selected,
    onClick = { TODO() },
)

预习

Although, you're probably better off in the long term making custom components you can control.虽然,从长远来看,您可能会更好地制作您可以控制的自定义组件。 Luckily, the source code is ready available.幸运的是,源代码已经准备就绪。 Just copy, paste and adjust to your needs.只需复制、粘贴并根据您的需要进行调整。

You could specify the Row height in the Row.Modifier like this:您可以在 Row.Modifier 中指定行高,如下所示:

 Row(
    Modifier
        .fillMaxWidth()
        //HERE YOU GO
        .height(30.dp)                
        .padding(horizontal = 0.dp, vertical = 0.dp)
   

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

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