LayoutInflater的使用 和getSystemService系統服務

在實際工作中,事先寫好的佈局文件往往不能滿足我們的需求,有時會根據情況在代碼中自定義控件,這就需要用到LayoutInflater。
LayoutInflater在Android中是“擴展”的意思,作用類似於findViewById(),不同的是LayoutInflater是用來獲得佈局文件對象的,而

findViewById()是用來獲得具體控件的。LayoutInflater經常在BaseAdapter的getView方法中用到,用來獲取整個View並返回。
LayoutInflater的用法有三種:

第一種方法:

 

view plaincopy to clipboard

  1. LayoutInflater inflater = LayoutInflater.from(this);  

  2. View layout = inflater.inflate(R.layout.main, null);  

[java] view plaincopy

  1. LayoutInflater inflater = LayoutInflater.from(this);  

  2. View layout = inflater.inflate(R.layout.main, null);  

 

 

 

第二種方法:

 

view plaincopy to clipboard

  1. LayoutInflater inflater = getLayoutInflater();  

  2. View layout = inflater.inflate(R.layout.main, null);  

[java] view plaincopy

  1. LayoutInflater inflater = getLayoutInflater();  

  2. View layout = inflater.inflate(R.layout.main, null);  

 

 

 

第三種方法:

 

view plaincopy to clipboard

  1. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  

  2. View layout = inflater.inflate(R.layout.main, null);  

[java] view plaincopy

  1. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  

  2. View layout = inflater.inflate(R.layout.main, null);  

 

 

 

第一種方法的本質就是調用第三種方法,而第二種方法和第三種方法有什麼區別,我還真不知道,有哪位知道的請留言指教啊!

下面是簡單的使用示例:

view plaincopy to clipboard

  1. public class LayoutInflaterActivity extends Activity {  

  2.     private EditText et;  

  3.     private Button btn;  

  4.   

  5.     @Override  

  6.     public void onCreate(Bundle savedInstanceState) {  

  7.         super.onCreate(savedInstanceState);  

  8.         // 第一種方法   

  9.         LayoutInflater inflater = LayoutInflater.from(this);  

  10.         View layout = inflater.inflate(R.layout.main, null);  

  11.         // 第二種方法   

  12.         // LayoutInflater inflater = getLayoutInflater();   

  13.         // View layout = inflater.inflate(R.layout.main, null);   

  14.         // 第三種方法   

  15.         // LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  

  16.         // View layout = inflater.inflate(R.layout.main, null);   

  17.         // 這裏是通過事先獲得的佈局文件來實例化具體控件,並且可以根據情況自定義控件   

  18.         et = (EditText) layout.findViewById(R.id.edittext);  

  19.         et.setBackgroundColor(Color.YELLOW);  

  20.         btn = (Button) layout.findViewById(R.id.btn);  

  21.         btn.setBackgroundColor(Color.CYAN);  

  22.         // 顯示   

  23.         setContentView(layout);  

  24.     }  

  25. }  

 

 

 

另外補充下,getSystemService是Activity中的方法,根據傳入的name來取得對應的服務對象,這些服務名稱參數都是Context類中的常量:

傳入的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                      WifiManager                  Wi-Fi服務
TELEPHONY_SERVICE            TeleponyManager         電話服務

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章