通常加载一个画面直接用setContView(r.layout.main);
我看到还有一种是:LayoutInflater factory = LayoutInflater.from(this);
view = factory.inflate(R.layout.main, null);
setContentView(view);请问一下,这两种方法有什么区别。
谢谢!

解决方案 »

  1.   

    实现结果一样的!LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.main, null);
    这段代码是为了得到view对象!操作layout!比如findViewById(..)方法,如果一个类直接继承view类的话,可以直接使用该方法!
    而如果不是view的派生类,就必须先得到view对象,才能调用view.findViewById(..)!!很多时候用到!比如自定义适配器时等等!楼主慢慢学习吧!
      

  2.   

    本质是一样的,都是给activity content set一个view
    第一种是是默认加载主布局R.layout.main
    第二种是从外部加载布局,也就是在setContentView(R.layout.main)之后即已经加载了主布局,在Adapter中一般用
    LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.main, null);
    加载convertview
    LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.main, null);
    setContentView(view);
    我想这样写为了过一段时间显示另一种布局,覆盖替换掉R.layout.main,而显示view中的内容如果加载的是同一个main.xml我觉得没必要用第二种,多写了2行代码