【发布时间】:2025-12-10 17:40:02
【问题描述】:
背景
在我的 Android 应用程序中,我以编程方式添加、删除和操作表格中的行元素。我需要根据这些表操作事件在 TableRow() 中设置元素的布局参数。
问题
从 Android 5.0 迁移到 Android 7.0 并在不同的平板电脑上运行应用程序后,表格行不反映定义的布局参数。
澄清一下:
配置一:
- 硬件:三星 Galaxy Tab4
- 安卓版本:5.0.2
- 结果:表格行的格式正确
配置2
硬件:三星 Galaxy Tab S2
安卓版本:7.0
结果:表格行未格式化
代码示例
//Define Layout Parameters
rowLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tableLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
editTextLayoutParams = new TableRow.LayoutParams(100, 40);
tvLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
//Create new table tow
tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(rowLayoutParams);
//Define Margins
int etMargin1 = (int)getActivity().getResources().getDimension(R.dimen.etMargin1);
int etMargin2 = (int)getActivity().getResources().getDimension(R.dimen.etMargin2);
int tvMargin1 = (int)getActivity().getResources().getDimension(R.dimen.tvMargin1);
int tvMargin2 = (int)getActivity().getResources().getDimension(R.dimen.tvMargin2);
if(editable)
{
//Create Edit Texts
EditText exampleEt = new EditText(getActivity());
editTextLayoutParams.setMargins(etMargin1,5,etMargin2,5);
exampleEt.setLayoutParams(editTextLayoutParams);
tableRow.setLayoutParams(rowLayoutParams);
tableRow.addView(exampleEt);
}
else
{
//Create Text Views
TextView exampleTv = new TextView(getActivity());
//Set Layout Params
tvLayoutParams.setMargins(tvMargin1,5,tvMargin2,5);
exampleTv.setLayoutParams(tvLayoutParams);
tableRow.setLayoutParams(rowLayoutParams);
tableRow.addView(exampleTv);
}
进一步规范
请注意代码示例中的编辑文本与文本视图。只有编辑文本元素的格式不正确。文本视图边距格式符合预期。
【问题讨论】:
标签: android android-layout android-version