ã¯ããã«
ããã«ã¡ã¯ã ãã¯ããªã³ã¯äºæ¥é¨ã§webã¢ããªã±ã¼ã·ã§ã³éçºãæ
å½ãã¦ãã西æã§ãð§âð»
ä»åã¯Jetpack Composeã®Modifieã«ã¤ãã¦ã¾ã¨ãã¦ã¿ããã¨æãã¾ãï¼
Jetpack Composeã¯ãAndroidéçºã«ããã¦Android Viewã«å¤ããæ°ããªUIãã¼ã«ãããã§ãã
ãã®ä¸ã§ãModifierã¯Composableé¢æ°ã®å¤è¦³ãåä½ãã«ã¹ã¿ãã¤ãºããããã®éè¦ãªè¦ç´ ã«ãªãã¾ãã
æ¬è¨äºã§ã¯ãæ§ã
ãªç¨®é¡ã®Modifierã¨ãã®ä½¿ç¨ä¾ã«ã¤ãã¦è§£èª¬ãããã¨æãã¾ãðââï¸
- ã¯ããã«
- Modifierã¨ã¯
- ãµã³ãã«ã³ã¼ã
- ã¬ã¤ã¢ã¦ãç³»Modifier
- æç»ç³»Modifier
- ã¸ã§ã¹ãã£ã¼ç³»Modifier
- Modifierã®é åºã®éè¦æ§
- ã«ã¹ã¿ã Modifierã®ä½æ
- æå¾ã«
Modifierã¨ã¯
Modifierã¯ãComposableé¢æ°ã«å¯¾ãã¦é åºä»ããããçµã¿åããå¯è½ãªè¦ç´ ã§ã
UIã³ã³ãã¼ãã³ãã®ã¬ã¤ã¢ã¦ããæç»ãã¸ã§ã¹ãã£ã¼ãªã©ãã«ã¹ã¿ãã¤ãºããããã«ä½¿ç¨ããã¾ãã
以ä¸ã®ä¾ã§ã¯Boxã®Modifierã«2ã¤ã®è¨å®ããã¦ãã¾ãã
Box( modifier = Modifier .padding(16.dp) .background(Color.Blue) ) { Text( text = "Hello Jetpack Compose", ) }
.padding(16.dp)ã¯Boxã«16dpã®Paddingãè¨å®
.background(Color.Blue)ã¯Boxã®èæ¯è²ã«éè²ãè¨å®
Modifierãªãã¸ã§ã¯ãã«ãã§ã¼ã³ããªããè¨å®ã追å ãã¦ãããã¨ã§UIã«æ§ã
ãªè¨å®ãè¡ããã¨ãå¯è½ã§ãï¼
以éã¯æ§ã
ãªç¨®é¡ã®Modifierã®ä¸ã§ããã使ç¨ããããªãã®ãããã¯ã¢ãããã¦ãç´¹ä»ãã¾ãã
ãµã³ãã«ã³ã¼ã
以éæ§ã
ãªModifierã®è¨å®ã説æããéã«ä½¿ç¨ããã³ã¼ãã§ãã
ããä¸æãªç®æãåºã¦ããå ´åã¯ãã¡ããã確èªä¸ããã
@Composable fun ProfileCard() { Card( shape = MaterialTheme.shapes.medium, colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.primaryContainer ), elevation = CardDefaults.cardElevation( defaultElevation = 4.dp ) ) { Row( modifier = Modifier .padding(16.dp) .fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { // ã¦ã¼ã¶ã¼æ å ± Column { // æ°å Text( text = "Yoshihiro Nishimura", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onPrimaryContainer ) // è·æ¥ Text( text = "Android Developer", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onPrimaryContainer ) Spacer(modifier = Modifier.height(8.dp)) // ãã¿ã³ OutlinedButton( onClick = { /* TODO: ã¢ã¯ã·ã§ã³ãå®ç¾© */ }, colors = ButtonDefaults.outlinedButtonColors( contentColor = MaterialTheme.colorScheme.primary ) ) { // ãã¿ã³ã®ã¢ã¤ã³ã³ç»å Icon( imageVector = Icons.Default.Email, contentDescription = stringResource(id = R.string.connect), modifier = Modifier.size(18.dp) ) Spacer(modifier = Modifier.width(8.dp)) // ãã¿ã³ã®ããã¹ã Text("Contact") } } // ãããã£ã¼ã«ã¢ã¤ã³ã³ Image( painter = painterResource(id = R.drawable.profile_icon), contentDescription = "Profile Image", contentScale = ContentScale.Crop, modifier = Modifier .size(72.dp) .clip(CircleShape) .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape) ) } } }
ã¬ã¤ã¢ã¦ãç³»Modifier
ã¬ã¤ã¢ã¦ãç³»Modifierã¯ãã³ã³ãã¼ãã³ãã®ãµã¤ãºãä½ç½®ãä½ç½ãªã©ãå¶å¾¡ããããã«ä½¿ç¨ãã¾ãã
padding
modifier = Modifier.padding(16.dp)
説æ
ã³ã³ãã¼ãã³ãã®å
å´ã¾ãã¯å¤å´ã«ä½ç½ã追å ãã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Rowè¦ç´ ã«è¨å®ãã¦ããããã¨ã§ã親ã®Cardè¦ç´ ã«å¯¾ãã¦16dpã®ä½ç½ãçã¾ãã¦ãã¾ãã
以ä¸ã®ããã«ä¸é¨ã®ã¿è¨å®ãããã¨ãæ°å¤ã調æ´ãããã¨ãå¯è½ã§ãã
modifier = Modifier.padding(start = 4.dp, end = 8.dp, bottom = 8.dp)
fillMaxWidth
modifier = Modifier.fillMaxWidth()
説æ
ã³ã³ãã¼ãã³ãã®å¹
ã親ã³ã³ãã¼ãã³ãã®æ大å¹
ã«åããã¦è¨å®ãã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Rowè¦ç´ ã«è¨å®ãã¦ããããã¨ã§ã親ã®Cardè¦ç´ ã«å¯¾ãã¦16dpã®ä½ç½ãå®ãã¤ã¤ã横å¹
ãæ大ã¾ã§åºãã¦ãã¾ãã
fillMaxWidthã«é¡ä¼¼ãã¦ããè¨å®ã¨ãã¦fillMaxHeightãfillMaxSizeãåããããªèãæ¹ã§ãã
heightã¨width
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.width(8.dp))
説æ
heightã¯é«ããæå®ããwidthã¯æ¨ªå¹
ãè¨å®ãã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Spacerè¦ç´ ã«è¨å®ãã¦ããããã¨ã§ã8dpã®ä½ç½ãçã¾ããããã«ãã¦ãã¾ãã
size
modifier = Modifier.size(72.dp)
説æ
ã³ã³ãã¼ãã³ãã®ç¸¦æ¨ªãµã¤ãºãåæã«è¨å®ãã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Imageè¦ç´ ã«è¨å®ãã¦ããããã¨ã§ã72dpã®ãããã£ã¼ã«ç»åã表示ãã¦ãã¾ãã
æç»ç³»Modifier
æç»ç³»Modifierã¯ãã³ã³ãã¼ãã³ãã®è¦ãç®ãå¤æ´ããããã«ä½¿ç¨ãã¾ãã
clip
modifier = Modifier.clip(CircleShape)
説æ
ã³ã³ãã¼ãã³ããç¹å®ã®å½¢ç¶ã«åãæããã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Imageè¦ç´ ã«è¨å®ãã¦ãããCircleShapeãå¼æ°ã«æ¸¡ãã¦ãããããç»åãåå½¢ã«åãæãããã¾ãã
注æç¹ã¨ãã¦ã¯ è¤éãªå½¢ç¶ã§å¤§ããªç»åãã³ã³ãã¼ãã³ããåãæããããã¨ããã¨ãæç»ããã©ã¼ãã³ã¹ã«å½±é¿ãåºãå ´åãããã¾ãã
ãã®ããå¿
è¦ã«å¿ãã¦é©åãªãµã¤ãºãå½¢ç¶ãé¸æããå¿
è¦ãããã¾ãã
border
modifier = Modifier.border(2.dp, MaterialTheme.colorScheme.primary, CircleShape)
説æ
ã³ã³ãã¼ãã³ãã«å¢çç·ã追å ãã¾ãã
ä»åã¯ãµã³ãã«ã³ã¼ãã®Imageè¦ç´ ã«è¨å®ãã¦ãããããåå½¢ã§åãæãããç®æã«å¢çç·ãæãã¦ãã¾ãã
ã¸ã§ã¹ãã£ã¼ç³»Modifier
ã¸ã§ã¹ãã£ã¼ç³»Modifierã¯ãã¦ã¼ã¶ã¼ã®ã¿ããæä½ãªã©ã®ã¤ã³ã¿ã©ã¯ã·ã§ã³ãå¦çããããã«ä½¿ç¨ãã¾ãã
clickable
modifier = Modifier.clickable { /* ã¯ãªãã¯æã®å¦ç */ }
説æ
ã³ã³ãã¼ãã³ããã¯ãªãã¯å¯è½ã«ããã¯ãªãã¯æã«å¦çãå®è¡ãããã¨ãã§ãã¾ãã
Scrollable
val state = rememberScrollState() Column( modifier = Modifier .fillMaxWidth() .verticalScroll(state), horizontalAlignment = Alignment.CenterHorizontally ) { repeat(10){ ProfileCard() } }
説æ
ã¹ã¯ãã¼ã«å¯¾è±¡ã®ã³ã³ãã³ãã®ãµã¤ãºã親ã³ã³ãã³ãã®ãµã¤ãºãè¶
ããå ´åã«ã¹ã¯ãã¼ã«ãããã¨ãã§ãã¾ãã
verticalScrollã¯ç¸¦æ¹åãhorizontalScrollã¯æ¨ªæ¹åã«ã¹ã¯ãã¼ã«ãããã¨ãã§ãã¾ãã
Modifierã®é åºã®éè¦æ§
Modifierã¯ãã§ã¼ã³ãã¦ããè¨å®ã®é åºã«ãã£ã¦çµæãå¤ããããã注æãå¿
è¦ã§ãã
clickableã¨paddingã®é åºãä¾ã«ãã¦ã¿ã¾ãã
ãã¿ã¼ã³â
Box( modifier = Modifier .clickable { /* ã¯ãªãã¯æã®å¦ç */ } .padding(16.dp) .background(color = Color.White) ) { Text( text = "Hello Jetpack Compose", ) }
ãã¿ã¼ã³â¡
Box( modifier = Modifier .padding(16.dp) .clickable { /* ã¯ãªãã¯æã®å¦ç */ } .background(color = Color.White) ) { Text( text = "Hello Jetpack Compose", ) }
2ã¤ã®ãã¿ã¼ã³ã®å·®ã¯clickableã¨paddingã®é åºã§ãã
ãã¿ã¼ã³â ã®å ´åã¯ç½èæ¯ã®é¨åããã¯ãªãã¯ã§ãã¾ãããã
ãã¿ã¼ã³â¡ã®å ´åã¯ãã¼ã¸ã³ã®ç¯å²å«ãã¦ã¯ãªãã¯ãããã¨ãã§ãã¾ãã
ãã¿ã¼ã³â ã®ã¯ãªãã¯ç¯å²
ãã¿ã¼ã³â¡ã®ã¯ãªãã¯ç¯å²
ããã¯Modifierã®è¨å®ã®åæ ããã§ã¼ã³ã§ç¹ãã é çªã«ãªãããã§ãã
ãã åã«è¨å®ããã ãã§ã¯æå¾
ããåä½ã«ãªããªããã¨ãå¤ãã®ã§ãè¨å®é ãèæ
®ããããã«ãã¾ããã
ã«ã¹ã¿ã Modifierã®ä½æ
é »ç¹ã«ä½¿ç¨ããModifierã®çµã¿åããã¯ãã«ã¹ã¿ã Modifierã¨ãã¦å®ç¾©ããã¨ä¾¿å©ã§ãã
以ä¸ã®ããã«Modifier.å称 ã§å®ç¾©ãããã¨ã§æ§ã
ãªç®æã§ä½¿ç¨ãããã¨ãã§ãã¾ãã
@Composable fun Modifier.profileIconStyle() = this .size(72.dp) .clip(CircleShape) .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape)
Image(
painter = painterResource(id = R.drawable.profile_icon),
contentDescription = "Profile Image",
contentScale = ContentScale.Crop,
modifier = Modifier.profileIconStyle()
)
æå¾ã«
ä»åã¯Jetpack Composeã®Modifierã«ã¤ãã¦ã¾ã¨ãã¾ããï¼
ä»åè¨äºãæ¸ãããããã§ç解ãæ·±ã¾ãé ã®ä¸ãæ´çã§ããæ°ããã¾ãã
ä»å¾ãé©åã«Modifierãè¨å®ãã¦ãæ§ã
ãªUIãæ§ç¯ãã¦ã¿ããã¨æãã¾ãï¼