Compose レイアウト モデルでは、AlignmentLine を使用してカスタムのアライメント ラインを作成できます。このラインは、親レイアウトが子を整列して配置するために使用できます。たとえば、Row は、子のカスタム配置ラインを使用して子を配置できます。
レイアウトが特定の AlignmentLine の値を提供する場合、レイアウトの親は、対応する Placeable インスタンスで Placeable.get 演算子を使用して、測定後にこの値を読み取ることができます。AlignmentLine の位置に基づいて、親は子の位置を決定できます。
Compose の一部のコンポーザブルには、すでにアライメント ラインが搭載されています。たとえば、BasicText コンポーザブルは、FirstBaseline と LastBaseline の配置ラインを公開します。
次の例では、firstBaselineToTop というカスタム LayoutModifier が FirstBaseline を読み取り、最初のベースラインから Text にパディングを追加します。
fun Modifier.firstBaselineToTop( firstBaselineToTop: Dp, ) = layout { measurable, constraints -> // Measure the composable val placeable = measurable.measure(constraints) // Check the composable has a first baseline check(placeable[FirstBaseline] != AlignmentLine.Unspecified) val firstBaseline = placeable[FirstBaseline] // Height of the composable with padding - first baseline val placeableY = firstBaselineToTop.roundToPx() - firstBaseline val height = placeable.height + placeableY layout(placeable.width, height) { // Where the composable gets placed placeable.placeRelative(0, placeableY) } } @Preview @Composable private fun TextWithPaddingToBaseline() { MaterialTheme { Text("Hi there!", Modifier.firstBaselineToTop(32.dp)) } }
この例の FirstBaseline を読み取るために、測定フェーズで placeable [FirstBaseline] が使用されます。
次の例では、paddingFrom を使用して、firstBaselineToTop の例のように、Text の最初のベースラインを上から 32 dp の位置に配置しています。テキストの最初と最後のベースラインからパディングするには、paddingFromBaseline を使用します。
@Composable private fun TextWithPaddingFromBaseline() { MaterialTheme { Text("Hi there!", Modifier.paddingFrom(FirstBaseline, before = 32.dp)) } }
Text。最初のベースラインの前に 32 dp のパディングがあります。行の子を配置する
Row では、alignByBaseline 修飾子と alignBy 修飾子を使用して、子をアライメント ラインに沿って配置します。どちらの修飾子も RowScope のメンバーであるため、Row のコンテンツなど、RowScope 内でのみ使用できます。整列する必要がある各子に修飾子を適用します。この属性がなく、align もない子要素は、Row の verticalAlignment に従います。
次の例では、サイズの異なる 2 つのテキストを最初のベースラインに揃えています。
@Composable private fun TitleWithSubtitle() { MaterialTheme { Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { Text( "Title", modifier = Modifier.alignByBaseline(), style = MaterialTheme.typography.headlineMedium ) Text( "Subtitle", modifier = Modifier.alignByBaseline(), style = MaterialTheme.typography.bodySmall ) } } }
Text 要素。alignByBaseline は alignBy(FirstBaseline) と同じです。配置ラインではない位置に配置するには、ブロックを alignBy に渡します。このブロックは、測定された子を受け取り、子の最上部から整列する線までの距離(ピクセル単位)を返します。次の例では、小さなボックスの中央が、隣のテキストのベースラインに揃えられています。
@Composable private fun BoxMiddleOnBaseline() { MaterialTheme { Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { Box( modifier = Modifier .size(8.dp) .background(Color.Gray) // Aligns the middle of the box with the text baseline. .alignBy { it.measuredHeight / 2 } ) Text( "Label", modifier = Modifier.alignByBaseline() ) } } }
Box。垂直方向の中心が、隣接する Text のベースラインに揃えられています。これらの修飾子を使用する子は 1 つのグループを形成します。Row は、グループの少なくとも 1 つの子を Alignment.Top があるかのように配置し、他の子を線が一致するように配置します。グループ内に 1 つだけ存在する子は、Alignment.Top が設定されているかのように配置されるため、何とも揃えられません。alignBy は align の形式であるため、2 つの修飾子は同じ子で連携して動作しません。
paddingFrom と alignBy は、異なる問題を解決します。paddingFrom は、独自のアライメント ラインに基づいて 1 つの要素にパディングを追加し、alignBy は兄弟要素を互いに揃えます。
Column では、alignBy は VerticalAlignmentLine またはブロックを受け取り、子を水平方向に配置します。ColumnScope には alignByBaseline がありません。
カスタムのアライメント ラインを作成する
カスタムの Layout コンポーザブルまたはカスタムの LayoutModifier を作成するときに、カスタムのアライメント ラインを指定して、他の親コンポーザブルがそれを使用して子を整列、配置できるようにすることができます。
次の例は、他のコンポーザブルがグラフデータの最大値と最小値に整列できるように 2 つのアライメント ライン、MaxChartValue と MinChartValue を公開するカスタム BarChart コンポーザブルを示しています。2 つのテキスト要素(Max と Min)が、カスタム配置ラインの中央に配置されています。
BarChart コンポーザブル。最大データ値と最小データ値に揃えられたテキスト。カスタム アライメント ラインは、プロジェクトの最上位変数として定義されます。
/** * AlignmentLine defined by the maximum data value in a [BarChart] */ private val MaxChartValue = HorizontalAlignmentLine(merger = { old, new -> min(old, new) }) /** * AlignmentLine defined by the minimum data value in a [BarChart] */ private val MinChartValue = HorizontalAlignmentLine(merger = { old, new -> max(old, new) })
サンプルを作成するためのカスタムのアライメント ラインは HorizontalAlignmentLine 型です。これは、子を垂直方向に配置するために使用されます。複数のレイアウトでこれらのアライメント ラインの値が提供される場合、マージポリシーがパラメータとして渡されます。Compose レイアウト システムの座標と Canvas 座標は [0, 0] を表しているため、左上隅と、x 軸、y 軸の正の方向はすべて下向きであり、MaxChartValue 値は常に MinChartValue より小さくなります。したがって、マージポリシーは、最大グラフデータ値のベースラインの場合は min、最小グラフデータ値のベースラインの場合は max です。
カスタムの Layout または LayoutModifier を作成する場合は、alignmentLines: Map<AlignmentLine, Int> パラメータを受け取る MeasureScope.layout メソッドでカスタムの配置ラインを指定します。
@Composable private fun BarChart( dataPoints: List<Int>, modifier: Modifier = Modifier, ) { val maxValue: Float = remember(dataPoints) { dataPoints.maxOrNull()!! * 1.2f } BoxWithConstraints(modifier = modifier) { val density = LocalDensity.current with(density) { // ... // Calculate baselines val maxYBaseline = // ... val minYBaseline = // ... Layout( content = {}, modifier = Modifier.drawBehind { // ... } ) { _, constraints -> with(constraints) { layout( width = if (hasBoundedWidth) maxWidth else minWidth, height = if (hasBoundedHeight) maxHeight else minHeight, // Custom AlignmentLines are set here. These are propagated // to direct and indirect parent composables. alignmentLines = mapOf( MinChartValue to minYBaseline.roundToInt(), MaxChartValue to maxYBaseline.roundToInt() ) ) {} } } } } }
このコンポーザブルの直接的および間接的な親は、アライメント ラインを使用できます。次のコンポーザブルは、カスタム レイアウトを作成し、2 つの Text スロットとデータポイントをパラメータとして受け取り、2 つのテキストをグラフデータの最大値と最小値に整列させます。このコンポーザブルのプレビューは、図 5 に示すとおりです。
@Composable private fun BarChartMinMax( dataPoints: List<Int>, maxText: @Composable () -> Unit, minText: @Composable () -> Unit, modifier: Modifier = Modifier, ) { Layout( content = { maxText() minText() // Set a fixed size to make the example easier to follow BarChart(dataPoints, Modifier.size(200.dp)) }, modifier = modifier ) { measurables, constraints -> check(measurables.size == 3) val placeables = measurables.map { it.measure(constraints.copy(minWidth = 0, minHeight = 0)) } val maxTextPlaceable = placeables[0] val minTextPlaceable = placeables[1] val barChartPlaceable = placeables[2] // Obtain the alignment lines from BarChart to position the Text val minValueBaseline = barChartPlaceable[MinChartValue] val maxValueBaseline = barChartPlaceable[MaxChartValue] layout(constraints.maxWidth, constraints.maxHeight) { maxTextPlaceable.placeRelative( x = 0, y = maxValueBaseline - (maxTextPlaceable.height / 2) ) minTextPlaceable.placeRelative( x = 0, y = minValueBaseline - (minTextPlaceable.height / 2) ) barChartPlaceable.placeRelative( x = max(maxTextPlaceable.width, minTextPlaceable.width) + 20, y = 0 ) } } } @Preview @Composable private fun ChartDataPreview() { MaterialTheme { BarChartMinMax( dataPoints = listOf(4, 24, 15), maxText = { Text("Max") }, minText = { Text("Min") }, modifier = Modifier.padding(24.dp) ) } }
あなたへのおすすめ
- 注: JavaScript がオフになっている場合はリンクテキストが表示されます
- Compose のグラフィック
- カスタム レイアウト {:#custom-layouts }
- Compose レイアウトの固有の測定値