简体   繁体   中英

Modify weight in the code doesn't display anything

I'm trying to create with code, 3 linearlayout with weight. Put them in a linearlayout, and put this last in a RelativeLayout. At the end I use this RelativeLayout in a Spinner (I say this in case is relevant).

The problem is when I try to put the weights, the RelativeLayout I obtain don't display anything (When I create the parameters, if I put WRAP_CONTENT instead 0, then is displayed, but of course ignores the weight).

public RelativeLayout createRL (final object objectSel, boolean opcion)
{
    if(opcion)
    {
        objectSel.setTimes(1);
    }

    RelativeLayout rl = new RelativeLayout(this);   

    LinearLayout llayout = new LinearLayout(this);

    llayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout f0 = new LinearLayout(this);
    f0.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout f1 = new LinearLayout(this);
    f1.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout f2 = new LinearLayout(this);
    f2.setOrientation(LinearLayout.HORIZONTAL);

    final TextView t0 = new TextView(this);
    final TextView t1 = new TextView(this);

    ImageButton ib1 = new ImageButton(this);
    ImageButton ib2 = new ImageButton(this);

    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    param.setMargins(0, 10, 0, 10);

    llayout.setLayoutParams(new ViewGroup.LayoutParams(param));     


    LinearLayout.LayoutParams param0 = new LinearLayout.LayoutParams(
            0,
           LinearLayout.LayoutParams.MATCH_PARENT);
    param0.weight=0.1f; 
    f0.setLayoutParams(new ViewGroup.LayoutParams(param0)); 


    LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.MATCH_PARENT);
    param1.weight=0.7f;
    f1.setLayoutParams(new ViewGroup.LayoutParams(param1)); 



    LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.MATCH_PARENT);
    param2.weight=0.2f;
    f2.setLayoutParams(new ViewGroup.LayoutParams(param2)); 



    t0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    t0.setTypeface(null,Typeface.BOLD);

    LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(               
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    param2.weight=1.0f;
    t1.setLayoutParams(new ViewGroup.LayoutParams(param3));

    t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);


    LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(               
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);

    ib1.setLayoutParams(new ViewGroup.LayoutParams(param4));
    ib1.setBackgroundResource(R.drawable.boton_mas_xml);


    LinearLayout.LayoutParams param5 = new LinearLayout.LayoutParams(               
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);

    ib2.setLayoutParams(new ViewGroup.LayoutParams(param5));    
    ib2.setBackgroundResource(R.drawable.boton_menos_xml);


    f0.setGravity(Gravity.CENTER_VERTICAL);
    f1.setGravity(Gravity.CENTER_VERTICAL);
    f2.setGravity(Gravity.RIGHT);


    t0.setText(Integer.toString(objectSel.getTimes())+"  ");
    t1.setText(objectSel.getNombre());
    f0.addView(t0);
    f1.addView(t1);
    f2.addView(ib1);
    f2.addView(ib2);

    llayout.addView(f0);
    llayout.addView(f1);
    llayout.addView(f2);    

    rl.setPadding(0, 0, 1, 3);
    rl.addView(llayout);
    return rl;

}

For linear layous you need to use LinearLayout.LayoutParams object.

MATCH_PARENT property breaks weight. Basicly use 0px fixed size to avoid calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM