How do I set the layout_constraintHorizontal_bias prop on a Composable that is in a Constraint Layout? Here is the XML code:
<TextView
...
tool:layout_constraintStart_toStartOf="parent"
tool:layout_constraintEnd_toEndOf="parent"
tool:layout_constraintWidth_max="wrap"
tool:layout_constraintHorizontal_bias="0"/>
Here is how my Jetpack Compose code looks right now:
ConstraintLayout(modifier = modifier.fillMaxSize()) {
val (button1, button2) = createRefs()
Button(
onClick = {},
modifier = Modifier.constrainAs(button1) {
top.linkTo(parent.top, margin = 16.dp)
}
) {
Text(text = "Button 1")
}
Button(
onClick = {},
modifier = Modifier.constrainAs(button2) {
top.linkTo(button1.bottom, margin = 4.dp)
start.linkTo(button1.end, margin = 20.dp)
end.linkTo(parent.end, margin = 20.dp)
width = Dimension.preferredWrapContent
}
) {
Text(text = "Button 2")
}
}
So my question is how do I set the horizontal bias of Button 2 to be 0?
Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.
To define a Barrier , you can select one or more View components from the “Design” view, open the “Guidelines” menu and select the Barrier . If you want to add it directly in the XML, you can use the following code snippet: The resulting layout looks like the screenshot of the “Design” layout editor view from below.
The value you set as a horizontal or vertical bias is a number between 0 and 1, representing a percentage, where the closest to 0 means the more biased to the left (horizontal) or the top constraint (vertical) and the closest to 1 means the more biased to the right (horizontal) or the bottom constraint (vertical).
These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.
You have to use the linkTo
function of the ConstrainScope
which has more parameters:
ConstraintLayout(modifier = modifier.fillMaxSize()) {
val (button1, button2) = createRefs()
Button(
onClick = {},
modifier = Modifier.constrainAs(button1) {
top.linkTo(parent.top, margin = 16.dp)
}
) {
Text(text = "Button 1")
}
Button(
onClick = {},
modifier = Modifier.constrainAs(button2) {
top.linkTo(button1.bottom, margin = 4.dp)
linkTo(button1.end, parent.end, startMargin = 20.dp, endMargin = 20.dp, bias = 0F)
width = Dimension.preferredWrapContent
}
) {
Text(text = "Button 2")
}
}
I guess that ConstraintLayout
in Jetpack Compose is not on par with the xml one yet and bias is simply missing. I found a workaround though - you can create a chain and chains actually do support bias (speaking about version 1.0.0-alpha04
).
For your example, something like this should do the trick:
ConstraintLayout(modifier = modifier.fillMaxSize()) {
// ...
createHorizontalChain(button2, chainStyle = ChainStyle.Packed(0F))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With