Agregar programáticamente tres botones en línea en un LinearLayout

I'm creating a LinearLayout programmatically and I'm adding to this layout three buttons, but they're showed one on top of the other.

How I can show the buttons in line?

Reading around I probably understood that I need to set up a LayoutParams but I didn't figured out how..

I've tried with this but it didn't did the trick..

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
);
ll.addView(b1, layoutParams);
ll.addView(b2, layoutParams);
ll.addView(b3, layoutParams);

Gracias por cualquier ayuda!

EDIT:

Probably I needed to add more details.

I have also other stuff in the Layout but I does'n matter, I've created an additional layout just for the buttons.

Now the buttons are in line but they have different width.. : /

I've tried with this but it didn't help..

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1f
);

preguntado el 30 de junio de 12 a las 13:06

Hey. I am trying to do something similar to you, and I am having some issues. I went through this solution and checked what you did, but for some reason I am getting 4 large buttons taking up the full width of the screen, and not appearing in the same row. Do you think you could have a look and tell me what I'm doing wrong? pastebin.com/26cTEqhg Gracias -

4 Respuestas

ll.setOrientation(LinearLayout.HORIZONTAL);

Lets you set buttons in horizontal line allignment.

But to give balanced space to all three buttons. you must set weight property for all the three Button objetos a 1.

Edit:

Do this for all buttons.

LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);

to set weight for all buttons.

Regards, Aqif Hamid

Respondido el 30 de junio de 12 a las 14:06

Yeah, that's right. Now I've my buttons in line but they're are of different width. How can I adjust the weight property? - Enriquecer

in your android layout, add this to your Buttonpropiedades de android:layout_weight="1" - Aqif Hamid

How can I this progammatically? - Enriquecer

Sorry to bother you man but it doesn't work.. the button.getLayoutParams() gives you a ViewGroup.LayoutParams that doesn't have the weight.. : / - Enriquecer

I did it! Just added this to each button: b2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1)); and setted the parent layout weigth to 3! ; ) - Enriquecer

Prueba esto:

ll.setOrientation(LinearLayout.HORIZONTAL);

Respondido el 30 de junio de 12 a las 13:06

All have MATCH_PARENT should have LayoutParams.WRAP_CONTENT in width or height (in at least one as per LinearLayout orientation )

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT

Respondido el 30 de junio de 12 a las 13:06

Pruébalo

    LinearLayout gvDivisao = (LinearLayout) findViewById(R.id.gvDivisao);
    LayoutInflater inflater = getLayoutInflater();
    LayoutParams btDivLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

    Button btDivA = (Button)inflater.inflate(R.layout.button_divisao, null);
    btDivA.setText("A");
    gvDivisao.addView( btDivA, btDivLayoutParams);

Respondido 16 Feb 13, 20:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.