Android中得到布局文件对象有三种方式

 

Android中得到布局文件对象有三种方式

第一种,通过Activity对象

View view = Activity对象.getLayoutInflater().inflater(R.layout.布局文件名,null);

 

第二种,通过Context对象

LayoutInflater lf  = (LayoutInflater)Context对象.getSystemServic(Context.LAYOUT_INFLATER_SERVICE);

View view = lf.inflate(R.layout.布局文件名,null);

(Context对象说明: 1、它是Activity和Service的父类;2、也可以通过View对象.getContext得到该对象)

 

第三种,还是通过Activity对象

注意,实现上Activity是Context的子类,所以Activity也可以像Content对象一样式去取得布局文件即

LayoutInflater lf  = (LayoutInflater)Activity对象.getSystemServic(Context.LAYOUT_INFLATER_SERVICE);

View view = lf.inflate(R.layout.布局文件名,null);

另解:

Android中得到布局文件对象有两种方式

第一种,在Activity所在类中
      this.getLayoutInflater().inflate(R.layout.布局文件名,null);
第二种,在非Activity所在类中
    Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate(R.layout.布局文件名,null);
**例如我在碎片中或者是adapter中获得布局:View view1=LayoutInflater.from(getActivity()).inflate(R.layout.layout_flipper,null);
 
posted @ 2016-06-14 09:40  风雨缠舟  阅读(1273)  评论(0编辑  收藏  举报