小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android中LayoutInflater的使用

 噢麥噶 2012-07-10

Android LayoutInflater



實(shí)際開(kāi)發(fā)中LayoutInflater這個(gè)類(lèi)還是非常有用的,它的 作用類(lèi)似于findViewById()。不同點(diǎn)是LayoutInflater是用來(lái)找res/layout/下的xml布局文件,并且實(shí)例化;而 findViewById()是找xml布局文件下的具體widget控件(如Button、TextView等)。
具體作用:
1、對(duì)于一個(gè)沒(méi)有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate()來(lái)載入;

2、對(duì)于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來(lái)獲得其中的界面元素。


LayoutInflater 是一個(gè)抽象類(lèi),在文檔中如下聲明:

public abstract class LayoutInflater extends Object  

 

獲得 LayoutInflater 實(shí)例的三種方式

1. LayoutInflater inflater getLayoutInflater();  //調(diào)用Activity的getLayoutInflater()

2. LayoutInflater localinflater =  (LayoutInflater)context.getSystemService

                                                 (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater LayoutInflater.from(context);   

 

其實(shí),這三種方式本質(zhì)是相同的,從源碼中可以看出:

getLayoutInflater():

Activity 的 getLayoutInflater() 方法是調(diào)用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:

 
  1. public PhoneWindow(Context context)  
  2.         super(context);  
  3.         mLayoutInflater LayoutInflater.from(context);  
  4. }  

可以看出它其實(shí)是調(diào)用 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;   

可以看出它其實(shí)調(diào)用 context.getSystemService()。

 

結(jié)論:所以這三種方式最終本質(zhì)是都是調(diào)用的Context.getSystemService()。

 

inflate 方法
通過(guò) sdk 的 api 文檔,可以知道該方法有以下幾種過(guò)載形式,返回值均是 View 對(duì)象,如下:

 
  1. public View inflate (int resource, ViewGroup root)  
  2. public View inflate (XmlPullParser parser, ViewGroup root)  
  3.   
  4. public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  
  5.   
  6. public View inflate (int resource, ViewGroup root, boolean attachToRoot)  

示意代碼:

 
  1. LayoutInflater inflater (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
  2.   
  3. View view inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));  
  4.   
  5. //EditText editText (EditText)findViewById(R.id.content);// error  
  6. EditText editText (EditText)view.findViewById(R.id.content);  

對(duì)于上面代碼,指定了第二個(gè)參數(shù) ViewGroup root,當(dāng)然你也可以設(shè)置為 null 值。


注意:

·inflate 方法與 findViewById 方法不同;

·inflater 是用來(lái)找 res/layout 下的 xml 布局文件,并且實(shí)例化;

·findViewById() 是找具體 xml 布局文件中的具體 widget 控件(如:Button、TextView 等)。


Inflater英文意思是膨脹,在Android中應(yīng)該是擴(kuò)展的意思吧。 

LayoutInflater 的作用類(lèi)似于 findViewById(),不同點(diǎn)是LayoutInflater是用來(lái)找layout文件夾下的xml布局文件,并且實(shí)例化!而 findViewById()是找具體某一個(gè)xml下的具體 widget控件(如:Button,TextView等)。

她可以有很多地方可以使用,如BaseAdapter的getView中,自定義Dialog中取得view中的組件widget等等。
它的用法有2種:

Java代碼
  1. view plaincopy to clipboardprint?  
  2. LayoutInflater inflater LayoutInflater.from(this);     
  3. View view=inflater.inflate(R.layout.ID, null);    
  4. 或者干脆并成一句:    
  5. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);    


另一種方法: 
Java代碼
  1. view plaincopy to clipboardprint?  
  2. LayoutInflater inflater (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
  3. View view=inflater.inflate(R.layout.ID, null);    
上面2種方法本質(zhì)上是一樣的,看下面的源碼,form()調(diào)用的就是getSystemService(): 
Java代碼
  1. Java代碼  
  2. public static LayoutInflater from(Context context)       
  3.     LayoutInflater LayoutInflater       
  4.             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
  5.     if (LayoutInflater == null      
  6.         throw new AssertionError("LayoutInflater not found.");       
  7.           
  8.     return LayoutInflater;       
  9.     
另外getSystemService()是Android很重要的一個(gè)API,它是Activity的一個(gè)方法,根據(jù)傳入的NAME來(lái)取得對(duì)應(yīng)的Object,然后轉(zhuǎn)換成相應(yīng)的服務(wù)對(duì)象。以下介紹系統(tǒng)相應(yīng)的服務(wù)。 

傳入的Name返回的對(duì)象說(shuō)明
WINDOW_SERVICEWindowManager管理打開(kāi)的窗口程序
LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定義的view
ACTIVITY_SERVICEActivityManager管理應(yīng)用程序的系統(tǒng)狀態(tài)
POWER_SERVICEPowerManger電源的服務(wù)
ALARM_SERVICEAlarmManager鬧鐘的服務(wù)
NOTIFICATION_SERVICENotificationManager狀態(tài)欄的服務(wù)
KEYGUARD_SERVICEKeyguardManager鍵盤(pán)鎖的服務(wù)
LOCATION_SERVICELocationManager位置的服務(wù),如GPS
SEARCH_SERVICESearchManager搜索的服務(wù)
VEBRATOR_SERVICEVebrator手機(jī)震動(dòng)的服務(wù)
CONNECTIVITY_SERVICEConnectivity網(wǎng)絡(luò)連接的服務(wù)
WIFI_SERVICEWifiManagerWi-Fi服務(wù)
TELEPHONY_SERVICETeleponyManager電話服務(wù)


Java代碼
  1. Java代碼  
  2. //基本用法    
  3. public void showCustomDialog(){    
  4.   AlertDialog.Builder builder;    
  5.   AlertDialog alertDialog;    
  6.   Context mContext AppActivity.this   
  7. //下面?zhèn)z種方法都可以    
  8.   //LayoutInflater inflater getLayoutInflater();    
  9.   LayoutInflater inflater (LayoutInflater)     
  10. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);    
  11.   View layout inflater.inflate(R.layout.custom_dialog,null);    
  12.   TextView text (TextView) layout.findViewById(R.id.text);    
  13.   text.setText("Hello, Welcome to Mr Wei's blog!");    
  14.   ImageView image (ImageView) layout.findViewById(R.id.image);    
  15.   image.setImageResource(R.drawable.icon);    
  16.   builder new AlertDialog.Builder(mContext);    
  17.   builder.setView(layout);    
  18.   alertDialog builder.create();    
  19.   alertDialog.show();    
  20.     
  21.    
  22.     
  23. protected void showToast(int type)      
  24.         Toast.makeText(this"*********"Toast.LENGTH_LONG).show();      
  25.       
  26.         LayoutInflater li (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
  27.         View view li.inflate(R.layout.toast, null);      
  28.               
  29.         Toast toast new Toast(this);      
  30.         toast.setView(view);      
  31.         toast.setDuration(type);      
  32.         toast.show();      
  33.      

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多