標籤:android layoutinflater
一.作用:
LayoutInflater作用是將layout的xml布局檔案執行個體化為View類對象,LayoutInflater 的作用類似於 findViewById(),不同點是LayoutInflater是用來找layout檔案夾下的xml布局檔案,並且執行個體化!而 findViewById()是找具體某一個xml下的具體 widget控制項(如:Button,TextView等)。
二.獲得 LayoutInflater 執行個體的三種方式
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater inflater = LayoutInflater.from(this);
3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);
其實,這三種方式本質是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該原始碼:
public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); }
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; }
可以看出它其實調用 context.getSystemService()。
結論:所以這三種方式最終本質是都是調用的Context.getSystemService()。
三.執行個體化LayoutInflater之後,就要將layout的xml布局檔案執行個體化為View類對象。
1.
LayoutInflater inflater = getLayoutInflater(); View view=inflater.inflate(R.layout.ID, null);
2.
LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null);
3.
LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);View view=inflater.inflate(R.layout.ID, null);
四.inflate 方法
通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
- public View inflate (int resource, ViewGroup root) (常用)
- inflate()方法一般接收兩個參數,第一個參數就是要載入的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null。這樣就成功成功建立了一個布局的執行個體,之後再將它添加到指定的位置就可以顯示出來了。
- public View inflate (XmlPullParser parser, ViewGroup root)
- public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
- public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代碼:
<pre name="code" class="java">LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); //EditText editText = (EditText)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content);
對於上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設定為 null 值。
執行個體:
下面我們就通過一個非常簡單的小例子,來更加直觀地看一下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>
這個布局檔案的內容非常簡單,只有一個空的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>
這個布局檔案也非常簡單,只有一個Button按鈕而已。
現在我們要想辦法,如何通過LayoutInflater來將button_layout這個布局添加到主布局檔案的LinearLayout中。根據剛剛介紹的用法,修改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); LayoutInflater layoutInflater = LayoutInflater.from(this); View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null); mainLayout.addView(buttonLayout); }}
可以看到,這裡先是擷取到了LayoutInflater的執行個體,然後調用它的inflate()方法來載入button_layout這個布局,最後調用LinearLayout的addView()方法將它添加到LinearLayout中。
現在可以運行一下程式,結果如所示:
Button 在介面上顯示出來了!說明我們確實是藉助LayoutInflater成功將button_layout這個布局添加到LinearLayout中了。 LayoutInflater技術廣泛應用於需要動態添加View的時候,比如在ScrollView和ListView中,經常都可以看到 LayoutInflater的身影。
參考:
http://www.cnblogs.com/merryjd/archive/2013/01/08/2851092.html
http://blog.csdn.net/guolin_blog/article/details/12921889
【邊做項目邊學Android】知識點:動態設定布局LayoutInflater