出處:http://blog.csdn.net/guolin_blog/article/details/12921889
相信接觸Android久一點的朋友對於LayoutInflater一定不會陌生,都會知道它主要是用於加載佈局的。而剛接觸Android的朋友可能對LayoutInflater不怎麼熟悉,因爲加載佈局的任務通常都是在Activity中調用setContentView()方法來完成的。其實setContentView()方法的內部也是使用LayoutInflater來加載佈局的,只不過這部分源碼是internal的,不太容易查看到。那麼今天我們就來把LayoutInflater的工作流程仔細地剖析一遍,也許還能解決掉某些困擾你心頭多年的疑惑。
先來看一下LayoutInflater的基本用法吧,它的用法非常簡單,首先需要獲取到LayoutInflater的實例,有兩種方法可以獲取到,第一種寫法如下:
- LayoutInflater layoutInflater = LayoutInflater.from(context);
- LayoutInflater layoutInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- layoutInflater.inflate(resourceId, root);
下面我們就通過一個非常簡單的小例子,來更加直觀地看一下LayoutInflater的用法。比如說當前有一個項目,其中MainActivity對應的佈局文件叫做activity_main.xml,代碼如下所示:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </LinearLayout>
那麼接下來我們再定義一個佈局文件,給它取名爲button_layout.xml,代碼如下所示:
- <Button xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button" >
- </Button>
- public class MainActivity extends Activity {
- private LinearLayout mainLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mainLayout = (LinearLayout) findViewById(R.id.main_layout);
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);
- mainLayout.addView(buttonLayout);
- }
- }
現在可以運行一下程序,結果如下圖所示:
Button在界面上顯示出來了!說明我們確實是藉助LayoutInflater成功將button_layout這個佈局添加到LinearLayout中了。LayoutInflater技術廣泛應用於需要動態添加View的時候,比如在ScrollView和ListView中,經常都可以看到LayoutInflater的身影。
當然,僅僅只是介紹瞭如何使用LayoutInflater顯然是遠遠無法滿足大家的求知慾的,知其然也要知其所以然,接下來我們就從源碼的角度上看一看LayoutInflater到底是如何工作的。
不管你是使用的哪個inflate()方法的重載,最終都會輾轉調用到LayoutInflater的如下代碼中:
- public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
- synchronized (mConstructorArgs) {
- final AttributeSet attrs = Xml.asAttributeSet(parser);
- mConstructorArgs[0] = mContext;
- View result = root;
- try {
- int type;
- while ((type = parser.next()) != XmlPullParser.START_TAG &&
- type != XmlPullParser.END_DOCUMENT) {
- }
- if (type != XmlPullParser.START_TAG) {
- throw new InflateException(parser.getPositionDescription()
- + ": No start tag found!");
- }
- final String name = parser.getName();
- if (TAG_MERGE.equals(name)) {
- if (root == null || !attachToRoot) {
- throw new InflateException("merge can be used only with a valid "
- + "ViewGroup root and attachToRoot=true");
- }
- rInflate(parser, root, attrs);
- } else {
- View temp = createViewFromTag(name, attrs);
- ViewGroup.LayoutParams params = null;
- if (root != null) {
- params = root.generateLayoutParams(attrs);
- if (!attachToRoot) {
- temp.setLayoutParams(params);
- }
- }
- rInflate(parser, temp, attrs);
- if (root != null && attachToRoot) {
- root.addView(temp, params);
- }
- if (root == null || !attachToRoot) {
- result = temp;
- }
- }
- } catch (XmlPullParserException e) {
- InflateException ex = new InflateException(e.getMessage());
- ex.initCause(e);
- throw ex;
- } catch (IOException e) {
- InflateException ex = new InflateException(
- parser.getPositionDescription()
- + ": " + e.getMessage());
- ex.initCause(e);
- throw ex;
- }
- return result;
- }
- }
當然,這裏只是創建出了一個根佈局的實例而已,接下來會在第31行調用rInflate()方法來循環遍歷這個根佈局下的子元素,代碼如下所示:
- private void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs)
- throws XmlPullParserException, IOException {
- final int depth = parser.getDepth();
- int type;
- while (((type = parser.next()) != XmlPullParser.END_TAG ||
- parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
- if (type != XmlPullParser.START_TAG) {
- continue;
- }
- final String name = parser.getName();
- if (TAG_REQUEST_FOCUS.equals(name)) {
- parseRequestFocus(parser, parent);
- } else if (TAG_INCLUDE.equals(name)) {
- if (parser.getDepth() == 0) {
- throw new InflateException("<include /> cannot be the root element");
- }
- parseInclude(parser, parent, attrs);
- } else if (TAG_MERGE.equals(name)) {
- throw new InflateException("<merge /> must be the root element");
- } else {
- final View view = createViewFromTag(name, attrs);
- final ViewGroup viewGroup = (ViewGroup) parent;
- final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
- rInflate(parser, view, attrs);
- viewGroup.addView(view, params);
- }
- }
- parent.onFinishInflate();
- }
這樣的話,把整個佈局文件都解析完成後就形成了一個完整的DOM結構,最終會把最頂層的根佈局返回,至此inflate()過程全部結束。
比較細心的朋友也許會注意到,inflate()方法還有個接收三個參數的方法重載,結構如下:
- inflate(int resource, ViewGroup root, boolean attachToRoot)
1. 如果root爲null,attachToRoot將失去作用,設置任何值都沒有意義。
2. 如果root不爲null,attachToRoot設爲true,則會在加載的佈局文件的最外層再嵌套一層root佈局。
3. 如果root不爲null,attachToRoot設爲false,則root參數失去作用。
4. 在不設置attachToRoot參數的情況下,如果root不爲null,attachToRoot參數默認爲true。
好了,現在對LayoutInflater的工作原理和流程也搞清楚了,你該滿足了吧。額。。。。還嫌這個例子中的按鈕看起來有點小,想要調大一些?那簡單的呀,修改button_layout.xml中的代碼,如下所示:
- <Button xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="300dp"
- android:layout_height="80dp"
- android:text="Button" >
- </Button>
其實這裏不管你將Button的layout_width和layout_height的值修改成多少,都不會有任何效果的,因爲這兩個值現在已經完全失去了作用。平時我們經常使用layout_width和layout_height來設置View的大小,並且一直都能正常工作,就好像這兩個屬性確實是用於設置View的大小的。而實際上則不然,它們其實是用於設置View在佈局中的大小的,也就是說,首先View必須存在於一個佈局中,之後如果將layout_width設置成match_parent表示讓View的寬度填充滿布局,如果設置成wrap_content表示讓View的寬度剛好可以包含其內容,如果設置成具體的數值則View的寬度會變成相應的數值。這也是爲什麼這兩個屬性叫作layout_width和layout_height,而不是width和height。
再來看一下我們的button_layout.xml吧,很明顯Button這個控件目前不存在於任何佈局當中,所以layout_width和layout_height這兩個屬性理所當然沒有任何作用。那麼怎樣修改才能讓按鈕的大小改變呢?解決方法其實有很多種,最簡單的方式就是在Button的外面再嵌套一層佈局,如下所示:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <Button
- android:layout_width="300dp"
- android:layout_height="80dp"
- android:text="Button" >
- </Button>
- </RelativeLayout>
OK!按鈕的終於可以變大了,這下總算是滿足大家的要求了吧。
看到這裏,也許有些朋友心中會有一個巨大的疑惑。不對呀!平時在Activity中指定佈局文件的時候,最外層的那個佈局是可以指定大小的呀,layout_width和layout_height都是有作用的。確實,這主要是因爲,在setContentView()方法中,Android會自動在佈局文件的最外層再嵌套一個FrameLayout,所以layout_width和layout_height屬性纔會有效果。那麼我們來證實一下吧,修改MainActivity中的代碼,如下所示:
- public class MainActivity extends Activity {
- private LinearLayout mainLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mainLayout = (LinearLayout) findViewById(R.id.main_layout);
- ViewParent viewParent = mainLayout.getParent();
- Log.d("TAG", "the parent of mainLayout is " + viewParent);
- }
- }
非常正確!LinearLayout的父佈局確實是一個FrameLayout,而這個FrameLayout就是由系統自動幫我們添加上的。
說到這裏,雖然setContentView()方法大家都會用,但實際上Android界面顯示的原理要比我們所看到的東西複雜得多。任何一個Activity中顯示的界面其實主要都由兩部分組成,標題欄和內容佈局。標題欄就是在很多界面頂部顯示的那部分內容,比如剛剛我們的那個例子當中就有標題欄,可以在代碼中控制讓它是否顯示。而內容佈局就是一個FrameLayout,這個佈局的id叫作content,我們調用setContentView()方法時所傳入的佈局其實就是放到這個FrameLayout中的,這也是爲什麼這個方法名叫作setContentView(),而不是叫setView()。
最後再附上一張Activity窗口的組成圖吧,以便於大家更加直觀地理解:
好了,今天就講到這裏了,支持的、吐槽的、有疑問的、以及打醬油的路過朋友儘管留言吧 ^v^ 感興趣的朋友可以繼續閱讀 Android視圖繪製流程完全解析,帶你一步步深入瞭解View(二) 。