微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在动态编程期间将 EditText 拟合到 Java 中的表格单元格?

如何解决如何在动态编程期间将 EditText 拟合到 Java 中的表格单元格?

问题:我很难让 EditText 字段适合表格单元格尺寸。

我尝试过的:我尝试了很多建议,如下面我的代码,在 StackOverflow 上有说明,但几乎所有的都是静态的而不是动态的例子。我看了几个差不多的视频。

要做什么:我要让用户选择将一行数据添加到表中并填写字段。以下代码运行良好,但我似乎找不到正确的方法来说明 LayoutParams 以适应单元格边界内的 EditText

for(int r=1; r<5;r++) {
   EditText editText = new EditText(this);
   editText.setBackgroundColor(Color.WHITE);
   editText.setTextSize(12);
   editText.setSingleLine();
   editText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
   row.addView(editText);
}

Table Cell EditText

<TableLayout
      android:id="@+id/table_authors"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="@color/colorDkGray"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toBottomOf="@id/btn_add_author"
      android:layout_marginTop="15dp"
      android:layout_marginEnd="5dp"
      android:layout_marginStart="5dp"/>

EditNote 活动中设置表格的代码

tableLayoutAuthors = findViewById(R.id.table_authors);
        tableLayoutAuthors.addView(setupAuthorsTableRow("+","Organization/First","Middle Name","Last Name","Suffix",true));

btnAddAuthor.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tableLayoutAuthors.addView(setupAuthorsTableRow("-","",false));
            }
        });

解决方法

您需要做一些调整:

  1. 在 xml TableLayout 中添加 android:stretchColumns="*" 表示拉伸所有列。

     <TableLayout
         android:id="@+id/table_authors"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginStart="5dp"
         android:layout_marginTop="15dp"
         android:layout_marginEnd="5dp"
         android:layout_marginBottom="716dp"
         android:orientation="vertical"
         android:stretchColumns="*"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
    
  2. 您需要将 editText 宽度设置为 0。因为使用 WRAP_CONTENT 它尊重它必须换行的内容宽度而不是单元格边界。

示例代码如下所示:

TableLayout tableLayoutAuthors  = findViewById(R.id.table_authors);
        TableRow row = new TableRow(this);
        for (int r = 1; r < 5; r++) {
            EditText editText = new EditText(this);
            editText.setBackgroundColor(Color.WHITE);
            editText.setTextSize(12);
            editText.setText("test");
            editText.setSingleLine();
            editText.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT));
            row.addView(editText);
        }
        tableLayoutAuthors .addView(row);
,

在 Mayur Gajra 的帮助下,我找到了解决问题的方法。下面发布的是解决我的问题的方法:

将此行添加到 XML:

declare
 i number:=1;
 j number:=1;
 begin
 while(i<=5) loop
   while (j<=i) loop
   dbms_output.put_line('*');``
   j:=j+1;
   end loop;
   dbms_output.new_line();
 i:=i+1;
 end loop;
 end;

更改为创建 android:stretchColumns="*" 的实例并设置边距和设置 TableRow.LayoutParams 的填充:

editText

Corrected Table Row with EditText

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。