[go: nahoru, domu]

blob: 6c00aa0393ba0cbdc7722e945540f18288c6ecbb [file] [log] [blame]
Mihai Popaf4b60202019-07-05 18:53:39 +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
20import androidx.compose.Composable
Mihai Popaf4b60202019-07-05 18:53:39 +010021import androidx.ui.core.Text
Matvei Malkov59bac362020-02-13 20:23:13 +000022import androidx.ui.foundation.Box
Mihai Popaf4b60202019-07-05 18:53:39 +010023import androidx.ui.graphics.Color
Mihai Popaf4b60202019-07-05 18:53:39 +010024import androidx.ui.layout.Column
Matvei Malkov59bac362020-02-13 20:23:13 +000025import androidx.ui.layout.LayoutAspectRatio
Adam Powell31c1ebd2020-01-09 09:48:24 -080026import androidx.ui.layout.LayoutHeight
Anastasia Sobolevad70e6702019-12-16 13:53:41 +000027import androidx.ui.layout.LayoutSize
Adam Powell31c1ebd2020-01-09 09:48:24 -080028import androidx.ui.layout.LayoutWidth
Mihai Popaf4b60202019-07-05 18:53:39 +010029import androidx.ui.layout.MaxIntrinsicHeight
30import androidx.ui.layout.MaxIntrinsicWidth
31import androidx.ui.layout.MinIntrinsicHeight
32import androidx.ui.layout.MinIntrinsicWidth
Anastasia Soboleva87365752019-12-03 18:13:45 +000033import androidx.ui.layout.Row
Anastasia Soboleva5c77fc52020-03-05 16:50:34 +000034import androidx.ui.layout.Stack
George Mount842c8c12020-01-08 16:03:42 -080035import androidx.ui.unit.dp
Mihai Popaf4b60202019-07-05 18:53:39 +010036
37/**
Matvei Malkov59bac362020-02-13 20:23:13 +000038 * Builds a layout containing three [Box] having the same width as the widest one.
Mihai Popaf4b60202019-07-05 18:53:39 +010039 *
40 * Here [MinIntrinsicWidth] is adding a speculative width measurement pass for the [Column],
41 * whose minimum intrinsic width will correspond to the preferred width of the largest
Matvei Malkov59bac362020-02-13 20:23:13 +000042 * [Box]. Then [MinIntrinsicWidth] will measure the [Column] with tight width, the same
Mihai Popa600ccf52020-03-03 14:18:37 +000043 * as the premeasured minimum intrinsic width, which due to [LayoutWidth.Fill] will force
Matvei Malkov59bac362020-02-13 20:23:13 +000044 * the [Box]'s to use the same width.
Mihai Popaf4b60202019-07-05 18:53:39 +010045 */
46@Sampled
47@Composable
48fun SameWidthBoxes() {
Anastasia Soboleva5c77fc52020-03-05 16:50:34 +000049 Stack {
Mihai Popaf4b60202019-07-05 18:53:39 +010050 MinIntrinsicWidth {
Adam Powell31c1ebd2020-01-09 09:48:24 -080051 Column(LayoutHeight.Fill) {
Matvei Malkov59bac362020-02-13 20:23:13 +000052 Box(
53 modifier = LayoutWidth.Fill + LayoutSize(20.dp, 10.dp),
54 backgroundColor = Color.Gray
55 )
56 Box(
57 modifier = LayoutWidth.Fill + LayoutSize(30.dp, 10.dp),
58 backgroundColor = Color.Blue
59 )
60 Box(
61 modifier = LayoutWidth.Fill + LayoutSize(10.dp, 10.dp),
62 backgroundColor = Color.Magenta
63 )
Mihai Popaf4b60202019-07-05 18:53:39 +010064 }
65 }
66 }
67}
68
Anastasia Soboleva87365752019-12-03 18:13:45 +000069/**
Mihai Popaf4b60202019-07-05 18:53:39 +010070 * Builds a layout containing two pieces of text separated by a divider, where the divider
71 * is sized according to the height of the longest text.
72 *
Anastasia Soboleva87365752019-12-03 18:13:45 +000073 * Here [MinIntrinsicHeight] is adding a speculative height measurement pass for the [Row],
Mihai Popaf4b60202019-07-05 18:53:39 +010074 * whose minimum intrinsic height will correspond to the height of the largest [Text]. Then
Anastasia Soboleva87365752019-12-03 18:13:45 +000075 * [MinIntrinsicHeight] will measure the [Row] with tight height, the same as the premeasured
Mihai Popa600ccf52020-03-03 14:18:37 +000076 * minimum intrinsic height, which due to [LayoutHeight.Fill] will force the [Text]s and
Mihai Popaf4b60202019-07-05 18:53:39 +010077 * the divider to use the same height.
78 */
79@Sampled
80@Composable
81fun MatchParentDividerForText() {
Anastasia Soboleva5c77fc52020-03-05 16:50:34 +000082 Stack {
Mihai Popaf4b60202019-07-05 18:53:39 +010083 MinIntrinsicHeight {
Anastasia Soboleva87365752019-12-03 18:13:45 +000084 Row {
85 Text(
86 text = "This is a really short text",
Adam Powell31c1ebd2020-01-09 09:48:24 -080087 modifier = LayoutFlexible(1f) + LayoutHeight.Fill
Anastasia Soboleva87365752019-12-03 18:13:45 +000088 )
Matvei Malkov59bac362020-02-13 20:23:13 +000089 Box(LayoutWidth(1.dp) + LayoutHeight.Fill, backgroundColor = Color.Black)
Anastasia Soboleva87365752019-12-03 18:13:45 +000090 Text(
91 text = "This is a much much much much much much much much much much" +
92 " much much much much much much longer text",
Adam Powell31c1ebd2020-01-09 09:48:24 -080093 modifier = LayoutFlexible(1f) + LayoutHeight.Fill
Anastasia Soboleva87365752019-12-03 18:13:45 +000094 )
Mihai Popaf4b60202019-07-05 18:53:39 +010095 }
96 }
97 }
98}
99
100/**
101 * Builds a layout containing three [Text] boxes having the same width as the widest one.
102 *
103 * Here [MaxIntrinsicWidth] is adding a speculative width measurement pass for the [Column],
104 * whose maximum intrinsic width will correspond to the preferred width of the largest
Matvei Malkov59bac362020-02-13 20:23:13 +0000105 * [Box]. Then [MaxIntrinsicWidth] will measure the [Column] with tight width, the same
Mihai Popa600ccf52020-03-03 14:18:37 +0000106 * as the premeasured maximum intrinsic width, which due to [LayoutWidth.Fill] modifiers will
Matvei Malkov59bac362020-02-13 20:23:13 +0000107 * force the [Box]s to use the same width.
Mihai Popaf4b60202019-07-05 18:53:39 +0100108 */
109@Sampled
110@Composable
111fun SameWidthTextBoxes() {
Anastasia Soboleva5c77fc52020-03-05 16:50:34 +0000112 Stack {
Mihai Popaf4b60202019-07-05 18:53:39 +0100113 MaxIntrinsicWidth {
Adam Powell31c1ebd2020-01-09 09:48:24 -0800114 Column(LayoutHeight.Fill) {
Matvei Malkov59bac362020-02-13 20:23:13 +0000115 Box(LayoutWidth.Fill, backgroundColor = Color.Gray) {
Mihai Popaf4b60202019-07-05 18:53:39 +0100116 Text("Short text")
117 }
Matvei Malkov59bac362020-02-13 20:23:13 +0000118 Box(LayoutWidth.Fill, backgroundColor = Color.Blue) {
Mihai Popaf4b60202019-07-05 18:53:39 +0100119 Text("Extremely long text giving the width of its siblings")
120 }
Matvei Malkov59bac362020-02-13 20:23:13 +0000121 Box(LayoutWidth.Fill, backgroundColor = Color.Magenta) {
Mihai Popaf4b60202019-07-05 18:53:39 +0100122 Text("Medium length text")
123 }
124 }
125 }
126 }
127}
128
Anastasia Soboleva87365752019-12-03 18:13:45 +0000129/**
130 * Builds a layout containing two [LayoutAspectRatio]s separated by a divider, where the divider
131 * is sized according to the height of the taller [LayoutAspectRatio].
Mihai Popaf4b60202019-07-05 18:53:39 +0100132 *
Anastasia Soboleva87365752019-12-03 18:13:45 +0000133 * Here [MaxIntrinsicHeight] is adding a speculative height measurement pass for the [Row], whose
134 * maximum intrinsic height will correspond to the height of the taller [LayoutAspectRatio]. Then
135 * [MaxIntrinsicHeight] will measure the [Row] with tight height, the same as the premeasured
Mihai Popa600ccf52020-03-03 14:18:37 +0000136 * maximum intrinsic height, which due to [LayoutHeight.Fill] modifier will force the
Anastasia Soboleva87365752019-12-03 18:13:45 +0000137 * [LayoutAspectRatio]s and the divider to use the same height.
Mihai Popaf4b60202019-07-05 18:53:39 +0100138 */
139@Sampled
140@Composable
141fun MatchParentDividerForAspectRatio() {
Anastasia Soboleva5c77fc52020-03-05 16:50:34 +0000142 Stack {
Mihai Popaf4b60202019-07-05 18:53:39 +0100143 MaxIntrinsicHeight {
Anastasia Soboleva87365752019-12-03 18:13:45 +0000144 Row {
Adam Powell31c1ebd2020-01-09 09:48:24 -0800145 val modifier = LayoutHeight.Fill + LayoutFlexible(1f)
Matvei Malkov59bac362020-02-13 20:23:13 +0000146 Box(modifier + LayoutAspectRatio(2f), backgroundColor = Color.Gray)
147 Box(LayoutWidth(1.dp) + LayoutHeight.Fill, backgroundColor = Color.Black)
148 Box(modifier + LayoutAspectRatio(1f), backgroundColor = Color.Blue)
Mihai Popaf4b60202019-07-05 18:53:39 +0100149 }
150 }
151 }
152}