您的位置:首页 > 其它

View 常见的两种布局方式

2016-11-16 10:17 309 查看

View 常见的两种布局方式

View view = LayoutInflater.from(context).inflate(R.layout.view_item.., parent, false);

View view =View.inflate(context,R.layout.view_item..,null);

1. 查看源码,可知 View.inflate 是对 LayoutInflater.from(context) 的封装

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}


2.继续深入 可得,布局通过获取系统的服务,来进行操作

public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: