LayoutInflater与findViewById的用法有很多可比较之处。
如:他们都可以通过id返回View。
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_dialog,null);
Button button = (Button) findViewById(R.id.button);
不同之处是:LayoutInflater是用来实例化整个布局文件,而findViewById()是实例化布局文中中的View。
//Three ways are OK.
LayoutInflater inflater = getLayoutInflater(); //Activity.getLayoutInflater() or Window.getLayoutInflater().
// LayoutInflater inflater = LayoutInflater.from(this); //Obtains the LayoutInflater from the given context.
// LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,null);