1.首先來說明一下LayoutInflater這個類中inflate()這個方法的作用:
inflate方法的主要作用就是將xml轉換成一個View對象,用於動態的創建佈局。它的作用類似於findViewById()。
不同點是LayoutInflater是用來找res/layout/下的xml佈局文件,並且實例化;而findViewById()是找xml佈局文件下的具體widget控件(如Button、TextView等)。
2.獲取LayoutInflater實例化的方法有如下三種:
1.通過SystemService獲得:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2.通過給定的context中獲得:
LayoutInflater inflater = LayoutInflater.from(context);
3.在activity中獲得:
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,實際上是View子類下window的一個函數)
這三種方式最終本質是都是調用的Context.getSystemService()。看一下該源代碼:
// 1.Activity的getLayoutInflater()方法是調用PhoneWindow的getLayoutInflater()方法。
publicPhoneWindow(Contextcontext) {
super(context);
mLayoutInflater= LayoutInflater.from(context);
}
// 可以看出它其實是調用LayoutInflater.from(context)。
2.LayoutInflater.from(context):
public static LayoutInflaterfrom(Context context) {
LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(LayoutInflater== null){
thrownew AssertionError("LayoutInflaternot found.");
}
returnLayoutInflater;
}
//可以看出它其實調用context.getSystemService()。
另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據傳入的NAME來取得對應的Object,然後轉換成相應的服務對象。
以下介紹系統相應的服務:
傳入的Name 返回的對象 說明
WINDOW_SERVICE WindowManager 管理打開的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater取得xml裏定義的view
ACTIVITY_SERVICE ActivityManager管理應用程序的系統狀態
POWER_SERVICE PowerManger電源的服務
ALARM_SERVICE AlarmManager鬧鐘的服務
NOTIFICATION_SERVICE NotificationManager狀態欄的服務
KEYGUARD_SERVICE KeyguardManager鍵盤鎖的服務
LOCATION_SERVICE LocationManager位置的服務,如GPS
SEARCH_SERVICE SearchManager搜索的服務
VEBRATOR_SERVICE Vebrator手機震動的服務
CONNECTIVITY_SERVICE Connectivity網絡連接的服務
WIFI_SERVICE WifiManagerWi-Fi服務
TELEPHONY_SERVICE TeleponyManager電話服務
3.inflate()方法的調用方式:
這個方法重載了四種調用方式,分別爲:
1. public View inflate(int resource, ViewGroup root);
2. public View inflate(int resource, ViewGroup root, boolean attachToRoot);
3.public View inflate(XmlPullParser parser, ViewGroup root);
4.public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot);
雖然重載了四個方法,但是這四種方法最終調用的,還是第四種方式。
第四種方式也很好理解,內部實現原理就是利用Pull解析器,對Xml文件進行解析,然後返回View對象。
inflate()方法中有三個參數,分別是:
1.resource 佈局的資源id
2.root 填充的根視圖
3.attachToRoot 是否將載入的視圖綁定到根視圖中
下面例出該方法的實際應用:
首先創建layout的xml文件
<!--?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="50dp"
android:background="@android:color/green"
android:gravity="center"
android:orientation="vertical">
<textview
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textcolor="@android:color/black"
android:textsize="20sp">
</textview>
</linearlayout>
Activity中的代碼如下:
public class TestActivity extends Activity {
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new MyAdapter(this));
}
private class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list, parent,false);
}
TextView tv = (TextView) convertView.findViewById(R.id.textView);
tv.setText(position + "");
return convertView;
}
}
}