我正在编写一个顶部有一个线性布局的活动,而后半部分有一个滚动视图。scrollView 在运行时可以获得多达 30 个 relativeLayouts。这可能吗?我已经尝试使用以下代码,但它给出了一个错误:
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus==false){
switch(v.getId()){
case 6:
main[no]=new RelativeLayout(this);
mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
main[no].setLayoutParams(mainParams);
mainLayout.addView(main[no]);
svParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
sv.setLayoutParams(svParams);
items[no]=new EditText(this);
rates[no]=new EditText(this);
quants[no]=new EditText(this);
items[no].setHint("Enter item name");
rates[no].setHint("Rate");
quants[no].setHint("Quantity");
RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rateParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams quantParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
items[no].setId(id++);
quantParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
quants[no].setId(id++);
pos=id;
rateParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
rates[no].setId(id++);
etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
items[no].setLayoutParams(etParams);
rates[no].setLayoutParams(rateParams);
quants[no].setLayoutParams(quantParams);
rates[no].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
quants[no].setInputType(InputType.TYPE_CLASS_NUMBER);
main[no].addView(items[no]);
main[no].addView(rates[no]);
main[no].addView(quants[no]);
quants[no].setOnFocusChangeListener(this);
sv.addView(main[no]);
no++;
break;
每个相对布局包含 3 个彼此相邻的 editText。