2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Jetpack Compose】Material3のButtonの高さを48dp未満に設定する

Last updated at Posted at 2023-07-31

次のようにボタンに高さを設定しても、何故か48dpまで余白が設定されてしまう。

Button(
    onClick = {},
    modifier = Modifier
        .border(width = 1.dp, color = Color.Blue)
        .heightIn(min = 36.dp)
) {
    Text("36dp")
}

image.png

対応策

LocalMinimumTouchTargetEnforcementfalseをセットする。(ExperimentalMaterial3Apiであることに注意)

CompositionLocalProvider(LocalMinimumTouchTargetEnforcement provides false) {
    Button(
        onClick = {},
        modifier = Modifier
            .border(width = 1.dp, color = Color.Blue)
            .heightIn(min = 36.dp)
    ) {
        Text("36.dp")
    }
}

image.png

詳細

ButtonはSurfaceを使って実装されているが、Surface内でタップ領域として48dpを確保している。
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Surface.kt;l=223;drc=f683eb9211f4df84f04e6437df3804135f8cf62e?hl=ja

2024/9/26 追記

仕様が変わったようです。LocalMinimumTouchTargetEnforcementではなく、 LocalMinimumInteractiveComponentSize で最小のタッチサイズ指定する形になりました。

CompositionLocalProvider(LocalMinimumInteractiveComponentSize provides 0.dp) {
    Button(
        onClick = {},
        modifier = Modifier
            .border(width = 1.dp, color = Color.Blue)
            .heightIn(min = 36.dp)
    ) {
        Text("36.dp")
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?