【发布时间】:2013-07-27 07:30:08
【问题描述】:
过去,每当我创建自定义表格布局时,我都会在 java 中创建子视图,然后使用 tableRow.addView(childView) 将它们添加到表格中。目前,我没有手动创建所有子视图,而是在 xml 中创建它们,然后将它们膨胀到 tableRow 中。
例如我这样做
RelativeLayout gameView = (RelativeLayout) mInflater.inflate(R.layout.my_game,
new RelativeLayout(context));
… //edits to gameView
gameView.requestLayout();
((ViewGroup) gameView.getParent()).removeAllViews();
tableRow.addView(gameView);
addView(tableRow);
但是我的问题是:如果我省略了((ViewGroup) gameView.getParent()).removeAllViews() 行,那么我会收到一个错误,即视图已经有一个父级,我必须首先在孩子的父级上调用revomeView。找不到任何名为removeView 的方法,我使用removeAllViews。但是当我这样做时,我在父级上得到一个 NullPointerException。
那么问题来了:我如何扩充视图然后将其添加到表格布局中?
【问题讨论】:
标签: android android-layout layout-inflater android-tablelayout