繁体   English   中英

通过使用FrameLayout扩展布局并将其添加到LinearLayout

[英]Inflating layout from withing FrameLayout and adding it to LinearLayout

我有layout.xml ,它是FrameLayout ,它里面还有另一个FrameLayout
我有Field子类化FrameLayout ,它包含逻辑,背景颜色处理等。

我想做这样的事情:

Field f = new Field(width, height, color); //args can be changed if needed.
linearLayout.addView(f);

在构造函数的Field类内部,我这样做:

LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.game_board_field, null);

某些内容已添加到linearLayout因为它的子级计数不为0,但我看不到Field是红色正方形。 我做错了什么吗?

是的,就是这样。

View view = inflate(getContext(), R.layout.fragment_game_board_field, this);

除此之外,我还必须更正xml中的其他属性,因为我放弃了Frament方法并决定手动添加布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM