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

2023-12-30

在表格布局中,我有一个表格行,在该表格行中,我有 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);

首先你应该知道一些事情:根据官方 Android 开发页面 http://developer.android.com/reference/android/view/View.html、View(以及从View派生的TextView)不支持Margin的设置,但是ViewGroups(比如LinearLayout, RelativeLayout等等...)做。

所以你可以做如下:

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

这会将所有子项的边距设置为 5 像素 - 我尝试过,它对我有用(尽管有一个LinearLayout垂直对齐)。尝试一下,如果我可以提供进一步帮助,请告诉我:)。

Cheers,

Ready4Fajir

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置编辑文本框的布局边距? 的相关文章

随机推荐