android LayoutInflater.inflate()学习

[color=blue]LayoutInflater.inflate()[/color]的作用就是[color=red]将一个用xml定义的布局文件查找出来[/color],注意与findViewById()的区别,[color=red]inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。[/color]

[size=medium][b]1.获取LayoutInflater对象有三种方法[/b][/size]

LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);


[color=blue][size=medium][b]2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义[/b][/size][/color]

[color=blue]resource[/color]:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。

[color=blue]root[/color]:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,[color=red]如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。[/color]

[color=blue]attachToRoot[/color]:[b]是否将root附加到布局文件的根视图上[/b]

Google上有关attachToRoot(也就是inflate()方法第三个参数)的文档太模糊:

被填充的层是否应该附在root参数内部?如果是false,root参数只是用于为XML根元素View创建正确的LayoutParams的子类。

其实意思就是:[color=red]如果attachToRoot是true的话,那第一个参数的layout文件就会被填充并附加在第二个参数所指定的ViewGroup内。[/color]方法返回结合后的View,根元素是第二个参数ViewGroup。[color=red]如果是false的话,第一个参数所指定的layout文件会被填充并作为View返回。[/color]这个View的根元素就是layout文件的根元素。不管是true还是false,都需要ViewGroup的LayoutParams来正确的测量与放置layout文件所产生的View对象。

attachToRoot传入true代表layout文件填充的View会被直接添加进ViewGroup,[color=blue]而传入false则代表创建的View会以其他方式被添加进ViewGroup。[/color]

让我们就两种情况多举一些例子来更深入的理解。

[size=medium][color=red][b]attachToRoot是True[/b][/color][/size]

假设我们在XML layout文件中写了一个Button并指定了宽高为match_parent。

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>


现在我们想动态地把这个按钮添加进Fragment或Activity的LinearLayout中。如果这里LinearLayout已经是一个成员变量mLinearLayout了,我们只需要通过如下代码达成目标:

inflater.inflate(R.layout.custom_button, mLinearLayout, true);


我们指定了用于填充button的layout资源文件,然后我们告诉LayoutInflater我们想把button添加到mLinearLayout中。[color=red]这里Button的LayoutParams种类为LinearLayout.LayoutParams。[/color]

下面的代码也有同样的效果。LayoutInflater的两个参数的inflate()方法自动将attachToRoot设置为true。

inflater.inflate(R.layout.custom_button, mLinearLayout);


另一种在attachToRoot中传递true的情况是使用自定义View。我们看一个layout文件中根元素有标签的例子。标签标识着这个layout文件的根ViewGroup可以有多种类型。


public class MyCustomView extends LinearLayout {
...
private void init() {
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);
}
}


这就是一个很好的使用attachToRoot的例子。这个例子中layout文件没有ViewGroup作为根元素,[color=red]所以我们指定我们自定义的LinearLayout作为根元素。[/color]如果layout文件有一个FrameLayout作为根元素而不是,[color=red]那么FrameLayout和它的子元素都可以正常填充[/color],而后都会被添加到LinearLayout中,LinearLayout是根ViewGroup,包含着FrameLayout和其子元素。

[size=medium][color=red][b]attachToRoot是False[/b][/color][/size]

我们看一下什么时候attachToRoot应该是false。在这种情况下,[size=large][color=red]inflate()方法中的第一个参数所指定的View不会被添加到第二个参数所指定的ViewGroup中。[/color][/size]

回忆一下刚才的例子中的Button,我们想通过layout文件添加自定义的Button至mLinearLayout中。当attachToRoot为false时,我们仍可以将Button添加到mLinearLayout中,但是这需要我们自己动手。

Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false);
mLinearLayout.addView(button);


[size=large][color=blue][b]总结[/b][/color][/size]

1.[color=red]如果可以传入ViewGroup作为根元素,那就传入它。[/color]
2.[b]避免将null作为根ViewGroup传入。[/b]
3.当我们不负责将layout文件的View添加进ViewGroup时设置attachToRoot参数为false。
4.[color=red]不要在View已经被添加进ViewGroup时传入true。[/color]
5.[color=blue]自定义View时很适合将attachToRoot设置为true。[/color]

转自:[url]http://www.codeceo.com/article/android-layoutinflater-inflate.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值