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

9

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

10

首先需要知道的一件事是:根据官方 Android 开发文档,View(包括 TextView)不支持设置 Margin,但 ViewGroup(例如 LinearLayoutRelativeLayout 等)支持。

所以你可以这样做:

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);
这将为所有子元素设置5像素的边距 - 我已经尝试过,并且对我有效(虽然使用了具有垂直对齐的LinearLayout)。试一试,让我知道是否还需要进一步帮助 :)。
干杯,
Ready4Fajir

4

编辑:
我建议使用下面的XML代码(当然,你需要更新id等信息)。这个XML代码中的"魔法"是它可以将所有可用的宽度平均分配给TextView(以及第二行的EditText)。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- The first "row" -->    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView 01"
            android:id="@+id/textView01" />

        <!-- Here you'd add your other five TextView's accordingly -->

    </LinearLayout>

    <!-- The second "row" -->    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView 01"
            android:id="@+id/editText01" />

        <!-- Here you'd add your other five EditText's accordingly -->

    </LinearLayout>
</LinearLayout>

在您的Java代码中,您可以像这样访问EditText视图:
EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");

我现在忽略了您需要以编程方式创建EditText的事实。你真的,真的需要用Java创建它们吗?(为什么?)
旧的答案: 如果您只想将布局边距设置为EditText视图,我猜您可以在LayoutParams变量上使用setMargins(left, top, right, bottom)函数调用。
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视图,我建议你查看以下帖子:2列TableLayout,每列恰好为50%

好的。什么是症状?预期行为和实际行为分别是什么? - dbm
实际上,我有一个表格布局。在这个布局中,我有两个表格行。在第一行中,我有六列,每一列都有一个文本视图。在第二行中,我有六个编辑框,并且我希望第二行的布局与第一行相同,也就是说具有相同的边距布局。我已经在XML中创建了第一行,但是无法在XML中创建第二行,因为我必须以编程的方式完成这个任务... - Arun
我现在根据 EDIT: 部分修改了我的答案。 - dbm
但是我必须用Java创建它,这是项目的需求。 - Arun
我是 stackoverflow 的新手,否则我可以向您展示我想要的截图。 - Arun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接