我试图用代码创建,3线性布局与重量。把它们放到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;
}
对于线性布局,你需要使用LinearLayout。LayoutParams对象。
MATCH_PARENT属性打破权重。基本上使用0px固定大小来避免计算。