brabo-hi
10/02/2021, 6:49 AMDaniele Segato
10/02/2021, 7:18 AMDaniele Segato
10/02/2021, 7:21 AMJan BĂna
10/02/2021, 10:55 AMmodifier = Modifier.fillMaxSize()
on the constraint layout and height = Dimension.fillToConstraints
in the constrainAs block of itemsRef. This is equivalent of layout_height="0dp"
in xml. Since the default value is wrap content, your list now fills the whole screen and the last item is hidden under the bottom section.
Another thing is that you don't need constraint layout for this at all. Simple Column with .weight(1F)
on items surface will be enough.brabo-hi
10/02/2021, 6:05 PM@Composable
fun ContraintLayoutComponent() {
ConstraintLayout {
val (topRef, itemsRef, bottomRef) = createRefs()
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(topRef) { top.linkTo(<http://parent.top|parent.top>) }, color = Color.Magenta) {
Text(text = "Top")
}
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(itemsRef) {
top.linkTo(topRef.bottom)
bottom.linkTo(<http://bottomRef.top|bottomRef.top>)
}, color = Color.Green) {
LazyColumn{
items(100) {
Text(text = "Body $it")
}
}
}
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(bottomRef) { bottom.linkTo(parent.bottom) }, color = Color.Red) {
Text(text = "Bottom")
}
}
}
On the preview i have body
and bottom
, however i am not getting the top
. What am i missing?
brabo-hi
10/02/2021, 6:06 PMbrabo-hi
10/02/2021, 6:08 PMColumn
with wight(1f) is working, however i am not able to get it working using constraint layout even when i change on body FillMaxSize()
and
height = Dimension.fillToConstraints
brabo-hi
10/02/2021, 6:09 PM@Composable
fun ContraintLayoutComponent() {
ConstraintLayout {
val (topRef, itemsRef, bottomBtn) = createRefs()
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(topRef) { top.linkTo(<http://parent.top|parent.top>) }
, color = Color.Magenta) {
Text(text = "Top")
}
Surface(modifier = Modifier
.fillMaxSize()
.constrainAs(itemsRef) {
top.linkTo(topRef.bottom)
bottom.linkTo(<http://bottomBtn.top|bottomBtn.top>)
height = Dimension.fillToConstraints
}, color = Color.Green) {
LazyColumn{
items(100) {
Text(text = "Body $it")
}
}
}
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(bottomBtn) { bottom.linkTo(parent.bottom) }
, color = Color.Red) {
Text(text = "Bottom")
}
}
}
Here is the resultJan BĂna
10/02/2021, 6:20 PM@Composable
fun ContraintLayoutComponent() {
ConstraintLayout(modifier = Modifier.fillMaxSize()) {
val (topRef, itemsRef, bottomRef) = createRefs()
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(topRef) { top.linkTo(<http://parent.top|parent.top>) }, color = Color.Magenta) {
Text(text = "Top")
}
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(itemsRef) {
top.linkTo(topRef.bottom)
bottom.linkTo(<http://bottomRef.top|bottomRef.top>)
height = Dimension.fillToConstraints
}, color = Color.Green) {
LazyColumn{
items(100) {
Text(text = "Body $it")
}
}
}
Surface(modifier = Modifier
.fillMaxWidth()
.constrainAs(bottomRef) { bottom.linkTo(parent.bottom) }, color = Color.Red) {
Text(text = "Bottom")
}
}
}
brabo-hi
10/02/2021, 6:39 PM