public View inflate(int resource, ViewGroup root, boolean attachToRoot)
參數(shù)解釋:
1.resource --> xml 資源的ID,例如R.layout.activity_main;
2.root --> 該參數(shù)可選,如果attachToRoot為true的情況下,root會作為resource的的父視圖.
3.attachToRoot resource資源是否需要裝載到root中.
package com.test.app;
/**
* 測試
* Created by fengwenhua on 2017/5/23.
*/
public class TestActivity extends AppCompatActivity {
@BindView(R.id.splash_rl_content)
public LinearLayout splash_rl_content;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ButterKnife.bind(this);
// testLayoutInflater();
testChildViewHeight();
testChildViewHeightNormal();
}
/**
* 以下代碼將導致系統(tǒng)崩潰
* inflate(R.layout.activity_splash,splash_rl_content,true)
* 究其原因,在attachToRoot為true且root不為空的情況下,view 已經(jīng)被添加到root中,已經(jīng)存在父視圖
*/
public void testLayoutInflater(){
View view = getLayoutInflater().inflate(R.layout.item_test,splash_rl_content,true);
// 以下代碼將導致如下異常,并導致系統(tǒng)崩潰
// java.lang.IllegalStateException: The specified child already has a parent.
// You must call removeView() on the child's parent first.
splash_rl_content.addView(view);//問題點
}
/**
* 以下代碼會導致item_test的寬高不起作用
*/
public void testChildViewHeight(){
View view = getLayoutInflater().inflate(R.layout.item_test,null);
splash_rl_content.addView(view);
}
/**
* 以下代碼會導致item_test的寬高不起作用
*/
public void testChildViewHeightNormal(){
View view = getLayoutInflater().inflate(R.layout.item_test,splash_rl_content,true);
// splash_rl_content.addView(view);
}
}