问题描述
问题:我很难让 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);
}
<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));
}
});
解决方法
您需要做一些调整:
-
在 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" />
-
您需要将
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