1

I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:

LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);

Button b = new Button (this);
ll.addView(b);

How can I do it? Thanks.

tatiana_c
  • 948
  • 7
  • 16
  • 31
  • Please check this: http://stackoverflow.com/questions/4641072/how-to-set-layout-weight-attribute-dynamically-from-code – strongmayer Jan 01 '12 at 11:09

2 Answers2

2

Try this:

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
    lp.weight = 0.25;
    b.setLayoutParams(lp);
Code Poet
  • 11,227
  • 19
  • 64
  • 97
0

You can pass it in as part of the LinearLayout.LayoutParams constructor:

LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT,
                             LayoutParams.MATCH_PARENT, 1.0f);
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • Hi infinity. I think I begun to get the idea of the LayoutParams. Thanks. – tatiana_c Jan 01 '12 at 12:16
  • @tatiana_c - if 'infinity's' answer worked for you, please be sure to mark the answer as accepted so that we can all benefit. – Gayot Fow Jan 02 '12 at 00:43
  • Hi Garry, do you mean to choose "yes" button in the question if it was useful for me? I am an new user in this site, Thanks. – tatiana_c Jan 02 '12 at 11:03
  • Garry, hi again. My question is not relevant any more, in other my post there are somebody explained me about. I'll do. Thanks. – tatiana_c Jan 02 '12 at 11:09