在TableRow内部使用的Android Inflate布局 [英] Android Inflate layout to use inside TableRow

查看:90
本文介绍了在TableRow内部使用的Android Inflate布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在android中动态构建表.但是,当我尝试为一个视图(例如table_cell.xml)添加一个视图并将其添加到我的表行中时,什么也没出现:

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

另一方面,如果我只是手动创建一个TextView,然后将其添加到该行中,它似乎可以正常工作.谁能帮我弄清楚为什么添加膨胀的布局不起作用?

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?

推荐答案

您应该尝试 getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true);

在这种情况下,您不应该在tableRow上手动添加tableCell视图,因为充气机方法会处理此问题,因为我们传递了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 ..

这篇关于在TableRow内部使用的Android Inflate布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆