【问题标题】:How to set the layout margins of edit text boxes?如何设置编辑文本框的布局边距?
【发布时间】:2011-06-16 10:23:47
【问题描述】:

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

【问题讨论】:

    标签: java android ellipse


    【解决方案1】:

    首先你要知道:根据Official Android Dev Pages,Views(以及一个派生自View的TextView)不支持Margin的设置,但是ViewGroups(如LinearLayoutRelativeLayout等.. .) 做。

    所以你可以做以下事情:

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

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

    干杯,

    Ready4Fajir

    【讨论】:

      【解决方案2】:

      编辑:
      我会尝试使用下面的 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-column TableLayout with 50% exactly for each column

      【讨论】:

      • 好的。有什么症状?预期的和实际的行为是什么?
      • 实际上我有一个表格布局。我有两个表格行。在第一个表行中,我有六列,每列都有每个文本视图。在第二行中,我有六个 cedittext 框,我希望第二行像第一行一样意味着相同的边缘布局。我在 xml 中创建了第一行。但是不能在 xml 中创建第二行 bcoz 我必须以编程方式进行...
      • 我现在已经根据 EDIT: 部分修改了我的答案
      • 但我必须在 java 中创建这是项目的需要
      • 我是stackoverflow的新手,否则我可以给你看我想要的截图
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2014-11-14
      • 2016-08-11
      • 2013-05-12
      • 1970-01-01
      • 2012-07-15
      相关资源
      最近更新 更多