1.LayoutInflater.from(this).inflate(layoutId, root, boolean);
2.LayoutInflater.from(this).inflate(layoutId, root);
佈局填充器,填充佈局時,不同參數會有不同的反差效果。很鬱悶,怎麼不是我想要的?
很久以前深入理解過,又總是忘記。下面已我自己的理解方式記錄下來,便於日後速查。
網上大神有總結:
1. 如果root為null,attachToRoot將失去作用,設置任何值都沒有意義。
2. 如果root不為null,attachToRoot設為true,則會給加載的佈局文件的指定一個父佈局,即root。
3. 如果root不為null,attachToRoot設為false,則會將佈局文件最外層的所有layout屬性進行設置,當該view被添加到父view當中時,這些layout屬性會自動生效。
4. 在不設置attachToRoot參數的情況下,如果root不為null,attachToRoot參數默認為true。
動態加載頁面
我關心的是:
1、當前的item局部是否添加到父容器中?
2、當前的佈局跟容器寬高屬性是否生效?
3、inflate()方法返回的是哪個容器?
做實驗得出結論:
1、
返回的view是父容器佈局(不是item佈局的跟容器,如例中的root佈局);
item跟佈局的寬高屬性生效(item佈局的跟容器寬高屬性生效);
已經添加到父容器中(item佈局已經添加到root中了)。
RelativeLayout root = (RelativeLayout) findViewById(R.id.other_layout);View view2 = LayoutInflater.from(this).inflate(R.layout.item, root, true);View view5 = LayoutInflater.from(this).inflate(R.layout.item, root);2、
返回R.layout.item頁面的跟佈局(item跟佈局);
跟佈局的寬高屬性生效;
未添加到父容器中。
RelativeLayout root = (RelativeLayout) findViewById(R.id.other_layout);View view1 = LayoutInflater.from(this).inflate(R.layout.item, root, false);3、返回R.layout.item頁面的跟佈局;
跟佈局的寬高屬性失效(僅寬高屬性失效,其他屬性生效);
未添加到父容器中。
View view3 = LayoutInflater.from(this).inflate(R.layout.item, null, false);View view4 = LayoutInflater.from(this).inflate(R.layout.item, null, true);View view6 = LayoutInflater.from(this).inflate(R.layout.item, null);
閱讀更多 快樂肥宅劇場 的文章
關鍵字: inflate LayoutInflater 容器