LayoutInflater的作用就是动态加载xml布局好的界面,类似于findViewById()来获取已经定义好的控件一样。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体view控件(如Button、TextView等)。

具体的使用方法:

        //先获取系统服务
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        //然后即可获取xml布局文件
        View v = inflater.inflate(R.layout.customer_layout, null);
        //已经加载的布局就可以所以操作了
        TextView text = (TextView) v.findViewById(R.id.text1);

 

相关文章:

  • 2022-12-23
  • 2021-03-31
  • 2021-10-10
  • 2021-10-16
  • 2022-01-07
  • 2021-09-30
猜你喜欢
  • 2021-07-10
  • 2022-12-23
相关资源
相似解决方案