Spanish [SOLUCIONADO] Colocación consecutiva de labels

tomky

Active Member
Licensed User
Hola.

Queremos que se vayan colocando las labels una junto a otra. Y si no hay suficiente espacio para la etiqueta siguiente, que se agregue en una nueva línea.

¿Hay alguna librería para ello en B4a, a la manera de la librería AutoLabelUI en Android?

Gracias.
 
Last edited:
Hola Tomky

No se si existe una libreria para eso, pero lo puedes hacer simplemente comparando el ancho de las labels con el ancho de pantalla, y si lo excede ponerla debajo. Algo asi:


Code: Copy
Dim AltoLabel, AnchoLabel, TopLabel, LeftLabel as int

AltoLabel = 50dip  ' pon tus valores
AnchoLabel = 400dip ' pon tus valores
TopLabel = 0  ' pon tus valores
LeftLabel = 0 ' pon tus valores

  for n = 1 to TotalLabel
     dim label1 as label
     label1.Initialize("ElEvento si quieres")
     Activity.AddView(label1,LeftLabel,TopLabel,AnchoLabel,AltoLabel)
     LeftLabel = LeftLabel + AnchoLabel
     if LeftLabel+AnchoLabel>100%x then
        TopLabel = TopLabel + AltoLabel
        LeftLabel = 0
     end if
  next

Si quieres puedes meterles unos Dip de separación entre anchos y entre altos, esto a gusto

Saludos
 
Last edited:
Gracias Bgsoft.
Funciona perfectamente con labels de ancho fijo.
Pero, ¿cómo hacerlo con labels que adaptan su tamaño automáticamente a su texto contenido, al ser
AnchoLabel = -1 para ello?
 
Hola Tomky

Puedes intentarlo asi:
Code: Copy
' cambia esta linea 
LeftLabel = LeftLabel + AnchoLabel

' por esta
LeftLabel = LeftLabel + label1.Width

Saludos
 
Hola Bgsoft.

No funciona.
Me coloca una label encima de otra con un mínimo desplazamiento.
Tanto AnchoLabel como Label1.Width me dicen que son -2.

Gracias.
 
Hola Tomky, te hice un pequeño ejemplo:


Code: Copy
Dim AltoLabel,   TopLabel, LeftLabel, Espacio As Int
Dim AnchoLabel As Float

AltoLabel = 50dip  ' pon tus valores
TopLabel = 0  ' pon tus valores
LeftLabel = 0 ' pon tus valores
Espacio = 5dip ' pon tus valores

   Dim bmp As Bitmap
   bmp.InitializeMutable(1,1) 'ignore
   cvs.Initialize2(bmp)
  For n = 1 To 10
    Dim label1 As Label
    label1.Initialize("Evento1")
    label1.TextSize = 20
    label1.Typeface = Typeface.DEFAULT

    ' es solo para cambiar el color del label y el largo del texto para verlo mejor
      If n Mod 2 = 0 Then
      label1.Text ="Texto mas largo"
        label1.Color = Colors.Green
      Else
         label1.Text ="Texto"  
       label1.Color = Colors.Red
      End If
  
  
   Activity.AddView(label1,LeftLabel,TopLabel,-2,AltoLabel)
        
  
  AnchoLabel = cvs.MeasureStringWidth(label1.Text, label1.Typeface,label1.TextSize)
    
   LeftLabel = LeftLabel + AnchoLabel + Espacio
   If LeftLabel>100%x Then
      TopLabel = TopLabel + AltoLabel + Espacio
      LeftLabel = 0
      label1.Top =TopLabel
      label1.Left = LeftLabel
      LeftLabel= AnchoLabel + Espacio
    End If
  Next





Saludos
 
Last edited:
Muchas gracias BgSoft!
Funciona perfectamente. :)

Partiendo de tu ejemplo, hemos añadido mejoras estéticas y funcionales:

Code: Copy
Sub Activity_Create(FirstTime As Boolean)

    Activity.Color = Colors.White

    Dim cd As ColorDrawable
    cd.Initialize2(Colors.Blue,15dip,0dip,Colors.Blue)

    Dim AltoLabel, TopLabel, LeftLabel, Espacio, TotalLabels As Int
    Dim AnchoLabel As Float

    AltoLabel = 35dip
    TopLabel = 5dip
    LeftLabel = 5dip
    Espacio = 15dip
    TotalLabels = 10

    Dim bmp As Bitmap
    Dim cvs As Canvas
    bmp.InitializeMutable(1,1) 'ignore
    cvs.Initialize2(bmp)

    For n = 1 To TotalLabels

        Dim Label1 As Label
        Label1.Initialize("Label1")
        Label1.TextSize = 16
        Label1.Typeface = Typeface.DEFAULT
        Label1.Background = cd
        Label1.Gravity = Gravity.CENTER
        Label1.Tag = n
        Label1.TextColor = Colors.White
        Label1.Padding = Array As Int (5dip, 0, 5dip, 0)
        SetElevation(Label1, 30.0)
    

        If n Mod 2 = 0 Then
            Label1.Text ="Texto mas largo"
        Else
            Label1.Text ="Texto"
        End If


        Activity.AddView(Label1,LeftLabel,TopLabel,-2,AltoLabel)
    

        AnchoLabel = cvs.MeasureStringWidth(Label1.Text, Label1.Typeface,Label1.TextSize)


        LeftLabel = LeftLabel + AnchoLabel + Espacio

        If LeftLabel > 100%x Then
            TopLabel = TopLabel + AltoLabel + Espacio
            LeftLabel = 5dip
            Label1.Top = TopLabel
            Label1.Left = LeftLabel
            LeftLabel = LeftLabel + AnchoLabel + Espacio
        End If
    Next

End Sub

Sub SetElevation(v As View, e As Float)

    Dim jo As JavaObject
    Dim p As Phone

    If p.SdkVersion >= 21 Then
        jo = v
        jo.RunMethod("setElevation", Array As Object(e))
    End If
End Sub

Sub Label1_Click

    Dim lbl As Label
    lbl = Sender

    ToastMessageShow("Label" & lbl.tag, False)
End Sub

Por si a alguno le interesa.

Saludos
 
Back
Top