I am trying to align 3 elements; one ImageView
and two TextView
, using for it WeightSum
.
I assign WeightSum = 20
and give the elements 1, 17 and 2 respectively. But instead of leaving me the full line, it seems to be putting me Weight = 1
in each of the elements. This being the result.
The orange/black elements should occupy the entire width of the lines, and stay halfway.
Code
for(int i=0;i<data.Count; i++)
{
//Líneas principales
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 125);
oLinea = new LinearLayout(this);
//oLinea.Orientation = Orientation.Horizontal;
oLinea.LayoutParameters = lp;
oLinea.WeightSum =20;
//Reinstancio los parametros, 0 para que tenga en cuenta el peso y 125 para la altura
lp = new LinearLayout.LayoutParams(0, 125);
//ImageView
ImageView iEstado = new ImageView(this);;
lp.Weight = 1;
iEstado.LayoutParameters = lp;
iEstado.SetScaleType(ImageView.ScaleType.FitCenter);
//TextView1
TextView tDescripcionGrupo = new TextView(this);
tDescripcionGrupo.Text = data[i].toString();//Esto es irrelevante
lp.Weight = 17;
tDescripcionGrupo.LayoutParameters = lp;
//TextView2
TextView tEstado = new TextView(this);
tEstado.Text = ""; //El String también es irrelevante.
lp.Weight = 2;
tEstado.LayoutParameters = lp;
//se añaden las listas a la línea
oLinea.AddView(iEstado);
oLinea.AddView(tDescripcionGrupo);
oLinea.AddView(tEstado);
//Se añaden las líneas a la layout activa.
activeLayout.AddView(oLinea);
}
I imagine that I have something wrong with the weights, or with some line parameter, but I've been trying for a long time and I still don't fully understand what needs to be done to make it Weight
work correctly.
The problem is that you are using the same instance of
LayoutParams
to define the weights of each of the elements.What you should do is define a new instance for each one: