LoginSignup
9
5

More than 1 year has passed since last update.

Jetpack Compose 枠線つきの角丸

Last updated at Posted at 2021-10-29

AndroidのJetpack Composeで大枠のComposeを枠線つきの角丸にする方法です。
この記事は以下のバージョンに対応しています。

バージョン
Jetpack Compose 1.0.4
AndroidStudio ArcticFox 2020.3.1 Patch 3

Viewの場合

Viewを枠線つきの角丸にする場合、下記のようなxmlファイルを作成し、Viewのbackgroundに定義しました。

rounded_corner_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <corners android:radius="20dp" />
  <stroke
    android:width="2dp"
    android:color="@color/black" />
</shape>

Jetpack Composeの場合

Row、Column、ConstraintLayoutといった大枠のComposeを枠線つきの角丸にする場合、下記のようなModifierをComposeのmodifierに定義することで実現できます。

Modifier
    .border(
        width = 2.dp,
        color = Color.DarkGray,
        shape = RoundedCornerShape(20.dp)
    )
    .background(
        color = Color.Gray,
        shape = RoundedCornerShape(20.dp)
    )

borderとbackground両方のshapeにRoundedCornerShapeを定義する点に注意が必要です。

RoundedHorizontalBar.kt
@Preview(showBackground = true)
@Composable
fun RoundedHorizontalBar() {
    Row(
        modifier = Modifier
            .height(60.dp)
            .fillMaxWidth()
            .padding(8.dp)
            .border(
                width = 2.dp,
                color = Color.DarkGray,
                shape = RoundedCornerShape(20.dp)
            )
            .background(
                color = Color.Gray,
                shape = RoundedCornerShape(20.dp)
            ),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            modifier = Modifier.padding(8.dp),
            imageVector = Icons.Rounded.Done,
            contentDescription = "Done"
        )
        Text(text = "テキスト")
    }
}

スクリーンショット 2021-10-29 17.01.14.png

9
5
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
9
5