Android Inflate layout to use inside TableRow

雨燕双飞 提交于 2019-12-02 13:03:26

问题


I'm trying to build a table in android dynamically. But when I attempt to inflate a view, for instance, table_cell.xml, and add it to my table row, nothing appears:

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

On the other hand, if I just create a TextView manually, then add it to the row, it seems to work fine. Can anyone help me figure out why adding an inflated layout doesn't work?


回答1:


You should try with getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true);

And you should not add the tableCell view on the tableRow manually in this case as the inflator method takes care of that since we pass true ..




回答2:


Try This You have to provide id for positioning.

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

or

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

or

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


来源:https://stackoverflow.com/questions/24661688/android-inflate-layout-to-use-inside-tablerow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!