第一种,通过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);
 

相关文章:

  • 2021-04-19
  • 2021-11-01
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-01
  • 2021-12-10
  • 2022-01-15
  • 2021-11-29
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案