Android LayoutInflater 參數(shù)詳情
之前已經(jīng)了解過了 layoutInflater.inflate 的三個(gè)參數(shù),但是一端是時(shí)間又忘記了,還是寫個(gè)博客吧
layoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot) 參數(shù)含義
如果root為null,attachToRoot將失去作用,設(shè)置任何值都沒有意義。
如果root不為null,attachToRoot設(shè)為true,則會(huì)給加載的布局文件的指定一個(gè)父布局,即root。
如果root不為null,attachToRoot設(shè)為false,則會(huì)將布局文件最外層的所有l(wèi)ayout屬性進(jìn)行設(shè)置,當(dāng)該view被添加到父view當(dāng)中時(shí),這些layout屬性會(huì)自動(dòng)生效。
在不設(shè)置attachToRoot參數(shù)的情況下,如果root不為null,attachToRoot參數(shù)默認(rèn)為true。
項(xiàng)目布局如下,
MainActivity 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="hello, world"
android:layout_width="match_parent"
android:layout_height="20dp" />
<FrameLayout
android:id="@+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
sub_view 布局
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="90dp"
android:text="Button" >
</Button>
詳解 root 為 null
首先 如果root為null 的時(shí)候,即:
layoutInflater.inflate(int resource, null)
此時(shí)調(diào)用的是:
layoutInflater.inflate(resource, root, root != null)
=》
layoutInflater.inflate(resource, null, false)
示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout = findViewById(R.id.main_layout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View buttonLayout = layoutInflater.inflate(R.layout.sub_view, null);
frameLayout.addView(buttonLayout);
}
此時(shí)會(huì)發(fā)現(xiàn) sub_view 中的 button 即使設(shè)置了 layout_width / layout_height 也是無效的,因?yàn)?sub_view 的父布局不存在,所以無法設(shè)置。
root不為null,attachToRoot設(shè)為true
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout = findViewById(R.id.main_layout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View inflate = layoutInflater.inflate(R.layout.sub_view, frameLayout, true);
}
此時(shí) sub_view 設(shè)置了父布局, 并且 attachToRoot 為 true 將會(huì)直接添加到 frameLayout 中,并且 sub_view 中的 button 可自由設(shè)置寬高
root不為null,attachToRoot設(shè)為false
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout = findViewById(R.id.main_layout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View inflate = layoutInflater.inflate(R.layout.sub_view, frameLayout, false);
frameLayout.addView(inflate);
}
此時(shí) sub_view 雖然設(shè)置了父布局,但是 attachToRoot 為 false , 沒有直接添加到 frameLayout 布局中,所以不會(huì)顯示在界面中。此時(shí)需要 frameLayout 主動(dòng)添加 sub_view 布局,這些layout屬性會(huì)自動(dòng)生效,顯示在界面上。效果如上圖
參考資料
Android LayoutInflater原理分析,帶你一步步深入了解View(一)
https://blog.csdn.net/guolin_blog/article/details/12921889