我有这段代码,我在其中将某些视图动态添加到 relativeLayout 中。问题是尽管在代码中使用了上述功能,但所有视图都相互重叠。
date=(EditText)findViewById(R.id.Date);
save=(Button)findViewById(R.id.Save);
save.setText("Confirm Purchase");
container=(RelativeLayout)findViewById(R.id.container);
items[no]=new AutoCompleteTextView(this);
RelativeLayout.LayoutParams p1=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p1.addRule(RelativeLayout.ALIGN_PARENT_START);
items[no].setId(id++);
container.addView(items[no],p1);
items[no].setHint("Enter Item");
quants[no]=new EditText(this);
RelativeLayout.LayoutParams p2=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p2.addRule(RelativeLayout.RIGHT_OF, items[no].getId());
quants[no].setId(id++);
container.addView(quants[no],p2);
quants[no].setHint("Quantity");
rates[no]=new EditText(this);
RelativeLayout.LayoutParams p3=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p3.addRule(RelativeLayout.RIGHT_OF, quants[no].getId());
rates[no].setId(id++);
container.addView(rates[no], p3);
rates[no].setHint("Rates");
totals[no]=new TextView(this);
RelativeLayout.LayoutParams p4=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p4.addRule(RelativeLayout.RIGHT_OF, rates[no].getId());
container.addView(totals[no], p4);
totals[no].setText("Amount");
请不要让我用 XML 来做。它是一段很长的代码。
XML:
<ScrollView android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container">
</RelativeLayout>
</ScrollView>