类似于findViewById(),findViewById()是找具体xml下的具体widget控件(如TextView、Button等),LayoutInflater找的是layout下的xml布局文件,并且实例化。
在一个Activity中,使用findViewById()的话,系统默认的是从onCreate()方法里setContentView()中的那个layout里寻找组件。
因此对于自定义的layout,使用findViewById()方法是会报错的(Null Point Exception)。对于这种情况,你必须先用inflate()将自定义的layout找出来,然后再用
这个layout的对象来找它上面的组件。
部分代码:
Context context = MyActivity.this;
LayoutInflater inflater = getLayoutInflater(); /*或者LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);*/
View myLayout = inflater.inflate(R.layout.xx, null);
TextView myText = (TextView)myLayout.findViewById(R.id.xx);