View 常见的两种布局方式

来源:互联网 发布:java中的 invoke 编辑:程序博客网 时间:2024/05/17 22:15

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;    }
0 0