现在的位置: 首页 > 综合 > 正文

从xml inflate自定义的View

2014年02月07日 ⁄ 综合 ⁄ 共 611字 ⁄ 字号 评论关闭

今天想自己实现一个View,这个View是常驻在程序中的,然后我会用自己的handler去刷新它。首先我想说的是这个View比较的复杂,其中有很多其他的子View。那么我想在它的构造函数中直接从xml中inflate出来这个View。那么该怎么做呢?

在这个View的构造函数中,加上下面这句话就可以实现上述要求:

LayoutInflater.from(context).inflate(R.layout.second, this,true);

这句话的意思就是:

将xml中的View树Inflate出来并且使用this提供的parent参数

boolean值的意思是:

Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root
view in the XML.

也就是当inflate之后,是否将这个this当作子View的parent并且将子View  attach上去。

由于android中的View只能有一个Parent,所以这个boolean的值还是很关键的,这意味着如果这个值是true的话,那么如果你想在其他的iewgroup上添加这个inflate出来的iew的话,你就必须在这个this上面remove这个iew。

【上篇】
【下篇】

抱歉!评论已关闭.