Android Linear Layout Weight Programmatically

后端 未结 4 2005
忘掉有多难
忘掉有多难 2020-12-09 15:47

I want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I co

4条回答
  •  忘掉有多难
    2020-12-09 16:20

    Here its the solution

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
        lp.weight = 1;
    

    See Full Solution

    LinearLayout ll1, ll2, ll3;
        /* Find these LinearLayout by ID 
         i.e ll1=(LinearLayout)findViewById(R.id.ll1);
         */
    
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
        lp.weight = 1;
        ll1.setLayoutParams(lp);
        ll2.setLayoutParams(lp);
        ll3.setLayoutParams(lp);
    

提交回复
热议问题