[go: nahoru, domu]

blob: 92cbc55f0a294cb994bdd8ff379898a537b20bd9 [file] [log] [blame]
Anastasia Soboleva1de1d102019-09-30 16:48:31 +01001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.ui.layout.samples
18
19import androidx.annotation.Sampled
Anastasia Soboleva1de1d102019-09-30 16:48:31 +010020import androidx.compose.Composable
Matvei Malkov59bac362020-02-13 20:23:13 +000021import androidx.ui.foundation.Box
Anastasia Soboleva1de1d102019-09-30 16:48:31 +010022import androidx.ui.graphics.Color
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010023import androidx.ui.layout.Column
George Mount842c8c12020-01-08 16:03:42 -080024import androidx.ui.layout.LayoutAlign
Adam Powell712dc992019-12-04 12:48:30 -080025import androidx.ui.layout.LayoutGravity
Adam Powell31c1ebd2020-01-09 09:48:24 -080026import androidx.ui.layout.LayoutHeight
Mihai Popa5f43e9d2020-02-12 14:14:46 +000027import androidx.ui.layout.LayoutSize
Adam Powell31c1ebd2020-01-09 09:48:24 -080028import androidx.ui.layout.LayoutWidth
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010029import androidx.ui.layout.Row
George Mount842c8c12020-01-08 16:03:42 -080030import androidx.ui.unit.dp
Anastasia Soboleva1de1d102019-09-30 16:48:31 +010031
32@Sampled
33@Composable
Anastasia Sobolevab8a60b92019-11-28 16:46:04 +000034fun SimpleAlignedModifier() {
Mihai Popa5f43e9d2020-02-12 14:14:46 +000035 // Here, the blue rectangle prefers to have a 20.dp size, subject to the incoming constraints.
36 // Because of the LayoutSize.Min modifier, if LayoutAlign was not present, the blue rectangle
37 // would actually be 40.dp x 40.dp to satisfy the min size set by the modifier. However,
38 // because we also provide LayoutAlign, the blue rectangle is allowed to be smaller than the min
39 // constraints, and it will be aligned in the 40.dp x 40.dp space. Note the example would not
40 // work if LayoutAlign was specified before LayoutSize in the modifier chain.
Matvei Malkov59bac362020-02-13 20:23:13 +000041 Box(
42 modifier = LayoutSize(20.dp) + LayoutSize.Min(40.dp, 40.dp) + LayoutAlign.TopCenter,
43 backgroundColor = Color.Blue
Mihai Popa5f43e9d2020-02-12 14:14:46 +000044 )
Anastasia Sobolevab8a60b92019-11-28 16:46:04 +000045}
46
47@Sampled
48@Composable
49fun SimpleVerticallyAlignedModifier() {
Mihai Popa5f43e9d2020-02-12 14:14:46 +000050 // Here, the blue rectangle prefers to have a 50.dp height, subject to the incoming constraints.
51 // Because of the LayoutSize.Fill modifier, if LayoutAlign was not present, the blue rectangle
52 // would actually fill the available height to satisfy the min height set by the modifier.
53 // However, because we also provide LayoutAlign, the blue rectangle is allowed to be smaller
54 // than the min height, and it will be centered vertically in the available height.
55 // The width of the rectangle will still fill the available width, because the
56 // LayoutAlign.CenterVertically modifier is only concerned with vertical alignment.
57 // Note the example would not work if LayoutAlign was specified before LayoutSize
58 // in the modifier chain.
Matvei Malkov59bac362020-02-13 20:23:13 +000059 Box(
60 LayoutSize(50.dp) + LayoutSize.Fill + LayoutAlign.CenterVertically,
61 backgroundColor = Color.Blue
Mihai Popa5f43e9d2020-02-12 14:14:46 +000062 )
Anastasia Sobolevab8a60b92019-11-28 16:46:04 +000063}
64
65@Sampled
66@Composable
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010067fun SimpleGravityInRow() {
Adam Powell31c1ebd2020-01-09 09:48:24 -080068 Row(LayoutHeight.Fill) {
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010069 // The child with no gravity modifier is positioned by default so that its top edge is
70 // aligned to the top of the vertical axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000071 Box(LayoutSize(80.dp, 40.dp), backgroundColor = Color.Magenta)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010072 // Gravity.Top, the child will be positioned so that its top edge is aligned to the top
73 // of the vertical axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000074 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.Top, backgroundColor = Color.Red)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010075 // Gravity.Center, the child will be positioned so that its center is in the middle of
76 // the vertical axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000077 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.Center, backgroundColor = Color.Yellow)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010078 // Gravity.Bottom, the child will be positioned so that its bottom edge is aligned to the
79 // bottom of the vertical axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000080 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.Bottom, backgroundColor = Color.Green)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010081 }
82}
83
84@Sampled
85@Composable
86fun SimpleGravityInColumn() {
Adam Powell31c1ebd2020-01-09 09:48:24 -080087 Column(LayoutWidth.Fill) {
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010088 // The child with no gravity modifier is positioned by default so that its start edge
89 // aligned with the start edge of the horizontal axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000090 Box(LayoutSize(80.dp, 40.dp), backgroundColor = Color.Magenta)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010091 // Gravity.Start, the child will be positioned so that its start edge is aligned with
92 // the start edge of the horizontal axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000093 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.Start, backgroundColor = Color.Red)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010094 // Gravity.Center, the child will be positioned so that its center is in the middle of
95 // the horizontal axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000096 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.Center, backgroundColor = Color.Yellow)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +010097 // Gravity.End, the child will be positioned so that its end edge aligned to the end of
98 // the horizontal axis.
Matvei Malkov59bac362020-02-13 20:23:13 +000099 Box(LayoutSize(80.dp, 40.dp) + LayoutGravity.End, backgroundColor = Color.Green)
Anastasia Soboleva29bef5e2019-10-22 16:50:15 +0100100 }
101}