這次跟大家分享的是關于LayoutInflater的使用,在開發的過程中,LayoutInfalter經常用于加載視圖,對,今天咱們來聊的就是,關于加載視圖的一些事兒,我記得之前一位曾共事過的一位同事問到我一個問題,activity是如何加載資源文件來顯示界面的,古話說得好,知其然不知其所以然,因此在寫這篇文章的時候我也做了不少的準備,在這里我先引出幾個問題,然后我們通過問題在源碼中尋找答案。
1.如何獲取LayoutInflater?
2.如何使用LayoutInflater?為什么?
3.Activity是如何加載視圖的?
4.如何優化我們的布局?
首先我們先看一下LayoutInflater是如何獲取的。
LayoutInflater inflater=LayoutInflater.from(context);
我們通過LayoutInflater.from(Context)獲取LayoutInflater,我們繼續進入LayoutInflater.java探索一番。
LayoutInflater.java:
/**
* Obtains the LayoutInflater from the given 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;
}
在LayoutInflater里面,通過靜態方法from(Context),然后繼續調用Context中的方法getSystemService獲取LayoutInflater,我們往Context繼續看。
Context.java:
public abstract Object getSystemService(@ServiceName @NonNull String name);
大家會發現,怎么點進去這是個抽象方法,其實Context是一個抽象類,真正實現的是ContextImpl這個類,我們就繼續看ContextImpl:
ContextImpl.java:
@Override
public Object getSystemService(String name) {
//繼續調用SystemServiceRegistry.getSystemService
return SystemServiceRegistry.getSystemService(this, name);
}
SystemServiceRegistry.java:
/**
* Gets a system service from a given context.
*/
public static Object getSystemService(ContextImpl ctx, String name) {
//先獲取ServiceFetcher,在通過fetcher獲取LayoutInflate
ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
return fetcher != null ? fetcher.getService(ctx) : null;
}
SystemServiceRegistry這個類中的靜態方法getSystemService,通過SYSTEM_SERVICE_FETCHERS獲取ServiceFetcher,我們先看看SYSTEM_SERVICE_FETCHERS跟ServiceFetcher在SystemServiceRegistry中的定義。
SystemServiceRegistry.java:
//使用鍵值對來保存
private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
new HashMap<String, ServiceFetcher<?>>();
/**
* Base interface for classes that fetch services.
* These objects must only be created during static initialization.
*/
static abstract interface ServiceFetcher<T> {
//只有一條接口,通過context獲取服務,先看一下其實現類
T getService(ContextImpl ctx);
}
SYSTEM_SERVICE_FETCHERS在SystemServiceRegistry這個類中作為全局常量,通過鍵值對的方式用來保存ServiceFetcher,而ServiceFetcher又是什么?在源碼中,ServiceFetcher是一條接口,通過泛型T定義了getService(ContextImpl)來獲取服務對象。那么具體ServiceFetcher具體的實現在什么地方?在SystemServiceRegistry中,有一段這樣的代碼:
SystemServiceRegistry.java:
static {
.....
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
new CachedServiceFetcher<LayoutInflater>() {
@Override
public LayoutInflater createService(ContextImpl ctx) {
return new PhoneLayoutInflater(ctx.getOuterContext());
}});
......
}
在這個靜態代碼塊中,通過registerService進行初始化注冊服務。我們先看看這個靜態方法。
/**
* Statically registers a system service with the context.
* This method must be called during static initialization only.
*/
private static <T> void registerService(String serviceName, Class<T> serviceClass,
ServiceFetcher<T> serviceFetcher) {
SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
}
registerService這個一段函數的作用就是用來通過鍵值對的方式,保存服務對象,也就是說,SystemServiceRegistry會初始化的時候注冊各種服務,而我們的也看到Context.LAYOUT_INFLATER_SERVICE作為key來獲取LayoutInfalter。
Context.java:
/**
* 定義這個常量,用于獲取系統服務中的LayoutInflate
* Use with {@link #getSystemService} to retrieve a
* {@link android.view.LayoutInflater} for inflating layout resources in this
* context.
*
* @see #getSystemService
* @see android.view.LayoutInflater
*/
public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";
我們繼續看看ServiceFetcher的實現類:
/**
* Override this class when the system service constructor needs a
* ContextImpl and should be cached and retained by that context.
*/
static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
private final int mCacheIndex;
public CachedServiceFetcher() {
mCacheIndex = sServiceCacheSize++;
}
@Override
@SuppressWarnings("unchecked")
public final T getService(ContextImpl ctx) {
final Object[] cache = ctx.mServiceCache;
synchronized (cache) {
// Fetch or create the service.
Object service = cache[mCacheIndex];
if (service == null) {
service = createService(ctx);
cache[mCacheIndex] = service;
}
return (T)service;
}
}
public abstract T createService(ContextImpl ctx);
}
CachedServiceFetcher的作用用于保存我們的泛型T,同時這個CachedServiceFetcher有一個抽象方法createService,createService這個方法用來創建這個服務,因此使用這個類就必須重寫這個方法,我們繼續看回:
ServiceFetcher.java:
staic{
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
new CachedServiceFetcher<LayoutInflater>() {
@Override
public LayoutInflater createService(ContextImpl ctx) {
return new PhoneLayoutInflater(ctx.getOuterContext());
}});
}
現在看回來這里,注冊服務不就是通過通過鍵值對的方式進行保存這個對象,然而我們獲取到的LayoutInflater其實是PhoneLayoutInflater。PhoneLayoutInflater繼承于LayoutInfalter.
小結:
我們獲取LayoutInflater對象,可以通過兩種方法獲取:
LayoutInflater inflater1=LayoutInflater.from(context);
LayoutInflater inflater2= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
context的實現類contextImpl,調用SystemServiceRegistry.getSystemService,通過鍵值對的方式獲取PhoneLayoutInflater對象,從中我們也看到,這種方式通過鍵值對的方式緩存起這個對象,避免創建過多的對象,這是也一種單例的設計模式。
現在咱們來看一下,我們是如何使用LayoutInflater來獲取View,我們先從一段小代碼看看。
我新建一個布局文件,my_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:text="我是一個按鈕">
</Button>
在布局文件中,我設置其layoutwidth與layout_height分別是填充屏幕。
在activity的content_main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ffzxcom.mytest.toucheventapplication.MainActivity"
tools:showIn="@layout/activity_main">
</RelativeLayout>
LayoutInflater.java:
方法一:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
方法二:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
//通過資源加載器和資源Id,獲取xml解析器
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
我們從代碼中看到,無論是方法一,還是方法二,最終還是會調用方法二進行加載,我們就從方法二的三個參數,進行分析一下。
@LayoutRes int resource 資源文件的Id
@Nullable ViewGroup root 根view,就是待加載view的父布局
boolean attachToRoot 是否加載到父布局中
從方法一看到,其實就是在調用方法二,只是方法一的第三個傳參利用root!=null進行判斷而已,實際上最終還是調用方法二。
我們先利用代碼進行分析一下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mContainer = (RelativeLayout) findViewById(R.id.content_main);
View view1 = LayoutInflater.from(this).inflate(R.layout.my_btn, null);
View view2 = LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer, false);
View view3 = LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer, true);
View view4 = LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer);
Log.e("view1:", view1 + "");
Log.e("view2:", view2 + "");
Log.e("view3:", view3 + "");
Log.e("view4:", view4 + "");
}
我們加載同一個布局文件my_btn.xml,獲取到view,然后分別輸出,觀察有什么不一樣:
view1:: android.support.v7.widget.AppCompatButton{27f4a822 VFED..C. ......I. 0,0-0,0}
view2:: android.support.v7.widget.AppCompatButton{14fb5dd2 VFED..C. ......I. 0,0-0,0}
view3:: android.widget.RelativeLayout{2a6bba10 V.E..... ......I. 0,0-0,0 #7f0c006f app:id/content_main}
view4:: android.widget.RelativeLayout{2a6bba10 V.E..... ......I. 0,0-0,0 #7f0c006f app:id/content_main}
問題來了,為什么我加載同一個布局,得到的view一個是Button,一個是RelativeLayout,我們每一個分析一下:
View1:
LayoutInflater.from(this).inflate(R.layout.my_btn, null);
我們看到,第二個參數root為空,也就是說實際上是調用方法二(root!=null):
LayoutInflater.from(this).inflate(R.layout.my_btn, null,false);
第三個參數attachToRoot 的意思是,是否把這個view添加到root里面,如果為false則不返回root,而是這個的本身,如果為true的話,就是返回添加view后的root.
因此,view1得到的是Button.
View2:
LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer, false);
同上可得,第三個參數attachToRoot 為false.也就是不把這個view添加到root里面去
因此,返回的是view2,就是Button.
View3:
LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer, true);
第三個參數為true,也就是意味待加載的view會附在root上,并且返回root.
因此,我們view3返回的是這個RelativeLayout,并且是添加button后的RelativeLayout.
View4:
LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer);
根據方法一跟方法二的比較,root!=null.view4跟view3的加載是一樣的,同理返回的是RelativeLayout.
根據以上的結論我們繼續往下面探究,我們通過LayoutInflater.from(this).inflate(R.layout.my_btn, null)獲取到了button,再把這個Button添加到mContainer中。再觀察一下效果,注意,這個按鈕的布局寬高是占全屏的。
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:text="我是一個按鈕">
</Button>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mContainer = (RelativeLayout) findViewById(R.id.content_main);
Button btn = (Button) LayoutInflater.from(this).inflate(R.layout.my_btn, null);
mContainer.addView(btn);
}
大家看到問題了嗎?為什么我在my_btn.xml中設置了button的布局寬高是全屏,怎么不起作用了?難道說在my_btn.xml中Button的layout_width和layout_height起不了作用?我們從源碼中看一下:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
//獲取資源加載器
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
//通過資源加載器和資源Id,獲取xml解析器
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
通過資源管理獲取xml解析器,繼續往下看:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
......
//保存傳進來的這個view
View result = root;
try {
// Look for the root node.
int type;
//在這里找到root標簽
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
//獲取這個root標簽的名字
final String name = parser.getName();
......
//判斷是否merge標簽
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");
}
//這里直接加載頁面,忽略merge標簽,直接傳root進rInflate進行加載子view
rInflate(parser, root, inflaterContext, attrs, false);
} else {
//通過標簽來獲取view
//先獲取加載資源文件中的根view
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
//布局參數
ViewGroup.LayoutParams params = null;
//關鍵代碼A
if (root != null) {
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
//temp設置布局參數
temp.setLayoutParams(params);
}
}
......
//關鍵代碼B
//在這里,先獲取到了temp,再把temp當做root傳進去rInflateChildren
//進行加載temp后面的子view
rInflateChildren(parser, temp, attrs, true);
......
//關鍵代碼C
if (root != null && attachToRoot) {
//把view添加到root中并設置布局參數
root.addView(temp, params);
}
//關鍵代碼D
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
......
} catch (Exception e) {
......
} finally {
......
}
return result;
}
}
在這一塊代碼中,先聲明一個變量result,這個result用來返回最終的結果,在我們的演示中,如果inflate(resource,root,isAttachRoot)中的root為空,那么布局參數params為空,并且根據關鍵代碼D可得,返回的result就是temp,也就是Button本身。因此在以上例子中,如果說root不為空的話,Button中聲明的layout_width與layout_height起到了作用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mContainer = (RelativeLayout) findViewById(R.id.content_main);
View view1 = LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer,false);
mContainer.addView(view1);
}
注:如果通過LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer)或者LayoutInflater.from(this).inflate(R.layout.my_btn, mContainer,true)加載視圖,不需要再額外的使用mContainer.addView(view),因為返回的默認就是root本身,在關鍵代碼C中可看到:
if (root != null && attachToRoot) {
root.addView(temp, params);
}
root會添加temp進去,在代碼初始化的時候,result默認就是root,我們不需要addView,在inflate中會幫我們操作,如果我們還要addView的話,就會拋出異常:
The specified child already has a parent. You must call removeView() on the child's parent first.
小結:
如果LayoutInflater的inflate中,傳參root為空時,加載視圖的根view布局寬高無效。反之根據關鍵代碼C與關鍵代碼A,分別對view進行設置布局參數。
咱們來看一下activity是如何加載視圖,我們從這一段代碼開始:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我們往setContentView繼續探索,找到Activity的setContentView()
/**
* Set the activity content from a layout resource. The resource will be
* inflated, adding all top-level views to the activity.
*
* @param layoutResID Resource ID to be inflated.
*
* @see #setContentView(android.view.View)
* @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
public void setContentView(@LayoutRes int layoutResID) {
//獲取窗口,設置contentView
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
getWindow()實際上是獲取window對象,但Window類是抽象類,具體的實現是PhoneWindow,我們往這個類看看:
PhoneWindow.java:
@Override
public void setContentView(int layoutResID) {
//判斷mContentParent是否為空,如果為空,創建
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
// 清空mContentParent 所有子view
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
……
} else {
//通過layoutInflate加載視圖
mLayoutInflater.inflate(layoutResID, mContentParent);
}
……
}
activity加載視圖,最后還是通過LayoutInflater進行加載視圖,activity的界面結構如下:
我們的mContentView就是ContentView,因此通過LayoutInflater加載視圖進入ContentView。而root就是mContentView,因此我們在Activity不需要自己addView().
總結:
知其然不知其所以然,這對于LayoutInflater描述再合適不過了,文章中本來還涉及到了關于如何使用LayoutInflater中遍歷view,代碼太長就不一一展示,而且在inflate中我們可以看到,通過使用merge標簽,可以減少view的層級,直接把merge標簽內的子view直接添加到rootview中,因此布局優化能提高視圖加載的性能,提高效率。還有獲取LayoutInflater的方式,通過鍵值對進行緩存LayoutInflater,這是在android中單例設計的一種體驗。