我正在尝试在 java 中定义 3 个彼此相邻的编辑文本。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// LinearLayout
mainLayout=(LinearLayout)findViewById(R.id.linearLayout);
// LinearLayout -> RelativeLayout
main=new RelativeLayout(this);
mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
main.setLayoutParams(mainParams);
mainLayout.addView(main);
// LinearLayout -> RelativeLayout -> EditText1
EditText item1=new EditText(this);
item1.setHint("Enter the item");
item1.setId(5);
RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
item1.setLayoutParams(etParams);
main.addView(item1);
// LinearLayout -> RelativeLayout -> EditText2
EditText quantity1=new EditText(this);
item1.setHint("Quantity");
item1.setId(6);
RelativeLayout.LayoutParams qparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etParams.addRule(RelativeLayout.ALIGN_LEFT, 5);
item1.setLayoutParams(qparams);
main.addView(quantity1);
// LinearLayout -> RelativeLayout -> EditText3
EditText rate1=new EditText(this);
item1.setHint("rate");
item1.setId(7);
RelativeLayout.LayoutParams rparams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etParams.addRule(RelativeLayout.ALIGN_RIGHT, 6);
item1.setLayoutParams(rparams);
main.addView(rate1);
`
我知道您可能会认为我也可以在 xml 中执行此操作,但问题是我必须在运行时创建更多编辑文本。
问题是所有的editTexts都相互重叠。请帮助