[英]How to add inflated view into custom tableLayout
In the past, whenever I create a custom table layout, I create the children views in java and then add them to the table using tableRow.addView(childView)
. 过去,每当我创建自定义表格布局时,都会在java中创建子视图,然后使用
tableRow.addView(childView)
将它们添加到表中。 Presently, instead of creating all my children views by hand, I created them in xml and then inflate them into the tableRow. 目前,我没有手动创建所有子视图,而是在xml中创建了它们,然后将它们充气到tableRow中。
So for example I do 所以例如
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);
My problem however is this: If I leave out the line ((ViewGroup) gameView.getParent()).removeAllViews()
then I get an error that the view already has a parent and that I must first call revomeView
on the child's parent. 但是我的问题是这样的:如果我忽略了
((ViewGroup) gameView.getParent()).removeAllViews()
那么我将收到一个错误消息,认为该视图已具有一个父对象,并且必须首先在该孩子的父revomeView
上调用revomeView
。 Not able to find any method called removeView
, I use removeAllViews
. 找不到任何名为
removeView
方法,我使用removeAllViews
。 But then when I do, I get a NullPointerException on the parent. 但是,当我这样做时,我在父级上得到了NullPointerException。
So the question: how do I inflate a view and then add it to a table layout? 那么问题来了:我该如何膨胀视图,然后将其添加到表布局中?
Try changing this line 尝试更改此行
RelativeLayout gameView = (RelativeLayout) mInflater.inflate(R.layout.my_game,
new RelativeLayout(context));
to 至
RelativeLayout gameView = (RelativeLayout) mInflater.inflate(R.layout.my_game,
null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.