Android 在动态编程期间,如何将EditText适配到Java中的表单元格?
问题:我很难将EditText字段放入表格单元格维度中 我尝试过的内容:我尝试过许多建议,如下面代码中关于StackOverflow的建议,但几乎所有建议都是静态而非动态示例。我看了几段差不多的视频 我想做的是:我想给用户一个选项,向表中添加一行数据并填充字段。下面的代码运行得很好,只是我似乎找不到正确的方法来说明Android 在动态编程期间,如何将EditText适配到Java中的表单元格?,android,android-edittext,tablerow,tablecell,Android,Android Edittext,Tablerow,Tablecell,问题:我很难将EditText字段放入表格单元格维度中 我尝试过的内容:我尝试过许多建议,如下面代码中关于StackOverflow的建议,但几乎所有建议都是静态而非动态示例。我看了几段差不多的视频 我想做的是:我想给用户一个选项,向表中添加一行数据并填充字段。下面的代码运行得很好,只是我似乎找不到正确的方法来说明LayoutParams,以使EditText适合单元格边界 for(int r=1; r<5;r++) { EditText editText = new EditTex
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
addandroid: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" />
在Mayur Gajra的帮助下,我找到了解决问题的方法。下面是修复我的问题的方法: 将此行添加到XML中:
android:stretchColumns="*"
更改为创建TableRow.LayoutParams的实例,并设置编辑文本的边距和填充:
for(int r=1; r<5;r++) {
EditText editText = new EditText(this);
// Change:
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams();
trLayoutParams.setMargins(3,3,3,3);
editText.setLayoutParams(trLayoutParams);
editText.setBackgroundColor(Color.WHITE);
editText.setTextSize(12);
editText.setGravity(Gravity.CENTER);
editText.setSingleLine();
// Change:
editText.setPadding(8, 8, 8, 8);
row.addView(editText);
row.setClickable(true);
}
用于(int r=1;rIs您的TableLayout
在xml文件中,或者甚至是在运行时添加的?您是否可以共享相关的行
&表
xml或运行时,以您使用的任何一个为准?@Mayur Gajra I添加了xml、设置表的代码以及管理表行的onClick
事件。注意,setupauthors选项卡leRow
方法管理标头和用户想要添加的任何add'l行的生成。谢谢,您关于添加拉伸列的建议很有效。但是对于TableRow LayoutParams“0”,我仍然无法使用边距和填充,并且EditText仍将作为我在问题正文中的上述图片。但是,我偶然发现了两个修复方法来纠正我的问题:1)行布局参数的setMargins,以及2)EditText的setPadding解决了我的问题。我把答案和你的建议放在一起。
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);
android:stretchColumns="*"
for(int r=1; r<5;r++) {
EditText editText = new EditText(this);
// Change:
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams();
trLayoutParams.setMargins(3,3,3,3);
editText.setLayoutParams(trLayoutParams);
editText.setBackgroundColor(Color.WHITE);
editText.setTextSize(12);
editText.setGravity(Gravity.CENTER);
editText.setSingleLine();
// Change:
editText.setPadding(8, 8, 8, 8);
row.addView(editText);
row.setClickable(true);
}