修改代码中的权重不会显示任何内容

时间:2014-06-02 19:14:14

标签: java android android-layout android-wrap-content

我尝试使用代码创建3 linelayout with weight。将它们放在linearlayout中,并将其放在RelativeLayout中。最后我在Spinner中使用了这个RelativeLayout(我说这是相关的)。

问题是当我尝试放置权重时,我获得的RelativeLayout不显示任何内容(当我创建参数时,如果我将WRAP_CONTENT改为0,则会显示,但当然忽略了重量。)

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;

}

1 个答案:

答案 0 :(得分:0)

对于线性布局,您需要使用LinearLayout.LayoutParams对象。

MATCH_PARENT属性打破了重量。基本上使用0px固定大小以避免计算。