我试图动态地在android中构建一个表。但是当我尝试给视图(例如table_cell.xml)充气并将其添加到我的表行时,没有任何内容出现:
TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
View tableCell = getActivity().getLayoutInflater()
.inflate(R.layout.table_cell, container, false);
TableRow tableRow = new TableRow(getActivity());
tableRow.addView(tableCell);
tableLayout.addView(tableRow);
另一方面,如果我只是手动创建一个TextView,然后将其添加到行,它似乎工作正常。任何人都可以帮我弄清楚为什么添加一个膨胀的布局不起作用?
答案 0 :(得分:2)
你应该尝试
getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true);
在这种情况下,您不应手动在tableCell
上添加tableRow
视图,因为我们传递的是真实的,因此充气程序方法会处理这个问题。
答案 1 :(得分:0)
试试这个 您必须提供定位ID。
TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
View tableCell = getActivity().getLayoutInflater()
.inflate(R.layout.table_cell, container, false);
TableRow tableRow = new TableRow(getActivity());
tableRow.addView(tableCell);
tableLayout.addView(tableRow,id);
或
TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
View tableCell = getActivity().getLayoutInflater()
.inflate(R.layout.table_cell, container, false);
TableRow tableRow = new TableRow(getActivity());
TableRow.LayoutParams LP = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(LP);
tablerow.setId(ID);
tableRow.addView(tableCell);
tableLayout.addView(tableRow);
或
TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
View tableCell = getActivity().getLayoutInflater()
.inflate(R.layout.table_cell, container, false);
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tablerow.setId(ID);
tableRow.addView(tableCell);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));