简体   繁体   English

Android Jetpack 组合 IconButton 填充

[英]Android Jetpack compose IconButton padding

How to remove padding in an IconButton?如何删除 IconButton 中的填充? I want items in my column have the same start padding我希望我的列中的项目具有相同的开始填充

预览图像

Column(
    modifier = Modifier
        .fillMaxWidth() 
        .padding(horizontal = 16.dp)
) {
    IconButton(onClick = { }) {
        Icon(asset = Icons.Filled.Search)
    }
    Text("Some text")
} 

With 1.0.0 the IconButton applies this modifier: IconButtonSizeModifier = Modifier.size(48.dp) .1.0.0IconButton应用了这个修饰符: IconButtonSizeModifier = Modifier.size(48.dp)
It is due to accessibility touch targets and allows the correct minimum touch target size.这是由于可访问性触摸目标并允许正确的最小触摸目标大小。

You can modify it using something like:您可以使用以下内容对其进行修改:

IconButton(modifier = Modifier.
        then(Modifier.size(24.dp)),
    onClick = { }) {
    Icon(
        Icons.Filled.Search,
        "contentDescription",
        tint = Color.White)
}

It is important the use of .then to apply the size in the right sequence.使用.then以正确的顺序应用size很重要。

在此处输入图片说明

Wrap the IconButton with CompositionLocalProvider to override the value of LocalMinimumTouchTargetEnforcement which enforces a minimum touch target of 48.dp .CompositionLocalProvider包装IconButton以覆盖LocalMinimumTouchTargetEnforcement的值,该值强制执行最小触摸目标48.dp

CompositionLocalProvider(
    LocalMinimumTouchTargetEnforcement provides false,
) {
    IconButton(onClick = { }) {
        Icon(
            imageVector = Icons.Filled.Search,
            contentDescription = "Search",
        )
    }
}

If you use IconButton just for handling click listener, instead of:如果您使用IconButton只是为了处理点击侦听器,而不是:

IconButton(onClick = { // Todo -> handle click }) {
    Icon(asset = Icons.Filled.Search)
}

You can use:您可以使用:

Icon(
    asset = Icons.Filled.Search,
    modifier = Modifier.clickable { // Todo -> handle click },
)

With this way you don't need to remove extra paddings.通过这种方式,您无需删除额外的填充。

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

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