Java 如何设置编辑文本框的布局边距?

Java 如何设置编辑文本框的布局边距?,java,android,ellipse,Java,Android,Ellipse,在表格布局中,我有一个表格行,在该表格行中,我有6个编辑文本框,我想为这6个编辑文本框设置布局边距 TableLayout t1=(TableLayout)findViewById(R.id.table_layout01); TableRow tr1=new TableRow(inventory.this); tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT))

在表格布局中,我有一个表格行,在该表格行中,我有6个编辑文本框,我想为这6个编辑文本框设置布局边距

TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);

  TableRow tr1=new TableRow(inventory.this);
  tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


  tr1.setBackgroundColor(Color.BLACK);
  EditText ed6=new EditText(inventory.this);
  //ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  /*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
  editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/


  ed6.setTextColor(Color.BLACK);
  ed6.setBackgroundColor(Color.WHITE);


  ed6.setText("1");
        tr1.addView(ed6);



  EditText ed7=new EditText(inventory.this);
  //ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed7.setTextColor(Color.BLACK);
  ed7.setBackgroundColor(Color.WHITE);
  ed7.setText("2");

  tr1.addView(ed7);

  EditText ed8=new EditText(inventory.this);
  //ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed8.setTextColor(Color.BLACK);
  ed8.setBackgroundColor(Color.WHITE);
  ed8.setText("3");

  tr1.addView(ed8);

  EditText ed9=new EditText(inventory.this);
  //ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed9.setTextColor(Color.BLACK);
  ed9.setBackgroundColor(Color.WHITE);
  ed9.setText("4");

  tr1.addView(ed9);

  EditText ed10=new EditText(inventory.this);
  //ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed10.setTextColor(Color.BLACK);
  ed10.setText("5");
  ed10.setBackgroundColor(Color.WHITE);

  tr1.addView(ed10);

  EditText ed11=new EditText(inventory.this);
  //ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed11.setTextColor(Color.BLACK);
  ed11.setText("6");
  ed11.setBackgroundColor(Color.WHITE);

  tr1.addView(ed11);

  t1.addView(tr1);

编辑:
我会尝试使用下面的XML(当然,您会更新id等)。xml中的“魔力”在于它将所有可用宽度均匀地分布在
TextView
(以及第二行的
EditText
)中

我现在忽略了这样一个事实,即您需要以编程方式创建EditText。你真的,真的需要用Java创建它们吗?(为什么?)

旧答案:
如果您只想将版面边距设置为EditText视图请求,可以使用
setMargins(左、上、右、下)
函数调用
LayoutParams
变量

int left = 6;
int top = 12;
int right = 6;
int bottom = 6;

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);

EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);

如果您最终希望在表行的六个EditText视图中平均分配所有可用空间,我建议您查看以下帖子:

首先,您应该知道:根据,视图(以及TextView派生自View)不支持边距设置,但支持视图组(如
LinearLayout
RelativeLayout
等)执行

因此,您可以做以下几点:

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);
这会将所有子项的边距设置为5个像素-我尝试过,它对我有效(尽管使用了垂直对齐的
线性布局)。试一试,让我知道是否可以进一步帮助:)

干杯


准备就绪4Fajir

正常。症状是什么?预期的和实际的行为是什么?实际上我有一个表布局。在这个布局中,我有两个表行。在第一个tablerow中,我有六列,每列都有一个textview。在第二行中,我有六个cedittext框,我希望第二行与第一行相同,意味着相同的边缘布局。我用xml创建了第一行。但是不能用xml创建第二行,因为我必须以编程方式来创建第二行……我现在已经根据编辑:部分修改了答案,但我必须用java创建它,这是projectI的需要stackoverflow oderwise新手,我可以给你看我想要的屏幕截图
int left = 6;
int top = 12;
int right = 6;
int bottom = 6;

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);

EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);