Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

281
Vistas
Compose Layout: Align relative to centered item (eg. toRightOf)

Is there a way in Compose to align a composable next to a centered item without using ConstraintLayout?

I want to achieve this: enter image description here

I could use a Spacer and Weights like this enter image description here

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically,
) {

    Spacer(Modifier.weight(1f))
    Button(...)
    Label(Modifier.weight(1f),...)
}

Problem is that I display the Label conditionally and if I hide the two elements with the weights, the button moves slightly.

Also not sure if using weights is producing more performance impact than the ConstraintLayout in the first place.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The Modifier.weight is the right way to create such a layout. I'm not sure if the performance is better than ConstraintLayout, but certainly not worse.

If you run into performance problems, you should create a problem on the google issue tracker, since that's how Compose is supposed to be used. Personally, I haven't encountered any performance problems related to weight, but the technology is fairly recent, so you can't completely rule out such a possibility.

In your case, you need to have some representation at any given time, to which you can apply Modifier.weight. You can use if-else and add Spacerinelsecase, but I prefer to useBox` with optional content: it looks cleaner and will work correctly in case you add animation.

Default Box contentAlignment is Alignment.TopStart, which will work exactly as needed in your case, but in some other cases, you can override it or add more Spacers inside.

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically,
) {
    Spacer(Modifier.weight(1f))
    Button(...)
    Box(Modifier.weight(1f)) { 
        if (condition) {
            Label(...)
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda