Chinaunix首页 | 论坛 | 博客
  • 博客访问: 400910
  • 博文数量: 78
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 940
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-25 09:39
文章分类

全部博文(78)

文章存档

2016年(7)

2015年(1)

2014年(35)

2013年(35)

我的朋友

分类: Android平台

2014-11-27 17:41:40

LayoutInflater主要是用于加载布局,广泛应用于动态添加ViewActivity中调用setContentView()方法来加载布局的,其内部也是使用LayoutInflater来加载布局的。

LayoutInflater的基本用法:

1.         首先需要获取到LayoutInflater的实例。

LayoutInflater layoutInflater = LayoutInflater.from(context);

或者

LayoutInflater layoutInflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

2.         获得LayoutInflater实例之后,调用它的inflate()方法来加载布局。

layoutInflater.inflate(resourceId, root);

inflate()方法一般接收两个参数,resourceId是要加载的布局idroot是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null

3.      创建了一个布局的实例后,将它添加到指定的位置显示。如:调用LinearLayoutaddView()方法将它添加到LinearLayout中。

使用layout_widthlayout_height来设置View的大小失效问题:

button_layout.xml文件:

阅读(3521) | 评论(0) | 转发(0) |
0

上一篇:Android自定义属性

下一篇:Android自定义View

给主人留下些什么吧!~~