Android Inflate布局在TableRow中使用

时间:2014-07-09 18:55:24

标签: android android-tablelayout

我试图动态地在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,然后将其添加到行,它似乎工作正常。任何人都可以帮我弄清楚为什么添加一个膨胀的布局不起作用?

2 个答案:

答案 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));