LayoutInflater.inflate()
inflate() v. 充氣,打氣
inflate就是將一個xml佈局變成View,
注意與findViewById()的區別,inflate是加載一個佈局文件,而findViewById則是從佈局文件中查找一個控件。
1. 獲取LayoutInflater對象有三種方法
1. LayoutInflater inflater=LayoutInflater.from(this);
2. LayoutInflater inflater=getLayoutInflater();
3. LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2. inflate(int resource, ViewGroup root, boolean attachToRoot)方法三個參數的含義
resource:需要加載的佈局文件的id
root:就是這個resourse要插入的佈局
attachToRoot:是否將這個生成的View添加到這個root中去
總結:
首先看帶三個參數的inflate方法:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1、如果root不爲null,且attachToRoot爲TRUE,則會在加載的佈局文件的最外層再嵌套一層root佈局(root.addView(temp, params)),
這時候xml根元素的佈局參數當然會起作用。
2、如果root不爲null,且attachToRoot爲false,則不會在加載的佈局文件的最外層再嵌套一層root佈局,
這個root只會用於爲要加載的xml的根view生成佈局參數(temp.setLayoutParams(params)),這時候xml根元素的佈局參數也會起作用了!!!
3、如果root爲null,則attachToRoot無論爲true還是false都沒意義!即xml根元素的佈局參數依然不會起作用!
示例:
LayoutInflater的使用中重點關注inflate方法的參數含義:
inflate(xmlId, null); 只創建temp的View,然後直接返回temp。
inflate(xmlId, parent); 創建temp的View,然後執行root.addView(temp, params);最後返回root。
inflate(xmlId, parent, false); 創建temp的View,然後執行temp.setLayoutParams(params);然後再返回temp。
inflate(xmlId, parent, true); 創建temp的View,然後執行root.addView(temp, params);最後返回root。
inflate(xmlId, null, false); 只創建temp的View,然後直接返回temp。
inflate(xmlId, null, true); 只創建temp的View,然後直接返回temp。
例子
用listview來驗證上面的理論
問題: 在ListView的item佈局中設置的高度沒有效果的問題。 item_lv_test.xml : <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dip" ***** android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" /> </LinearLayout> adapter的getView方法 : public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, null); //設置100dp是無效的 //這樣就可以了 convertView = inflate(R.layout.item_lv_test, parent, false); } return convertView; }
用LinearLayout驗證上面的問題
要添加的View的佈局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="80dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:background="@android:color/holo_green_dark" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_media_menu_icon" android:layout_height="24dp" android:layout_width="24dp" android:src="@drawable/ic_mv" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/tv_media_menu_text" android:text="bxbxbai" style="@style/Menu_TextView"/> </RelativeLayout> Java代碼中: LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, null); layout.addView(view); 這樣寫的話,你就會發現佈局文件R.layout.layout_menu_item中的android:layout_width="80dp"不起作用!!也就是說View.inflate方法忽略了佈局文件的寬度設置 下面改寫代碼: LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, layout); layout.addView(view); 你就會發現這樣寫會崩潰!然後下面這樣寫就沒問題了: LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, layout); View.inflate方法自動將生成的View添加到了這個ViewGroup root中去了!! 解釋: * View.inflate(Context context, int resource, ViewGroup root), * 這個方法本質上是調用了LayoutInflater.from(context).inflate(resource, root, root != null),在這個inflate方法中可以找到下面代碼: if (root != null && attachToRoot) { root.addView(temp, params); } 可見inflate方法自動將這個生成的View添加到了這個root中去了
一般規律
如果要加載的資源xml文件設置了參數(layout_*),則需要設置root,如果沒有,則不需要。
如果資源佈局是這樣的話,需要設置parent,否則佈局中的layout_*參數不起作用.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Text Test"
android:background="#ffa0a00c"/>
- 我們經常使用View的layout_ width和layout_ height來設置View的大小,而且一般都可以正常工作,所以有人時常認爲這兩個屬性就是設置View的真實大小一樣;然而實際上這些屬性是用於設置View在ViewGroup佈局中的大小的;這就是爲什麼Google的工程師在變量命名上將這種屬性叫作layout_ width和layout_ height,而不是width和height的原因了。
因爲layout_ height是相對了父級設置的,而此temp的getLayoutParams爲null。
-
如果資源佈局是這樣的話,什麼都不用管
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Text Test"
android:background="#ffa0a00c"/>
</LinearLayout>