android動態執行個體化xml布局的3種方法

來源:互聯網
上載者:User

標籤:

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

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

第一種方法:

 

[java] view plaincopy 
  1. LayoutInflater inflater = LayoutInflater.from(this);  
  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);  
 

第三種方法:

 

[java] view plaincopy 
  1. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
  2. View layout = inflater.inflate(R.layout.main, null);  

 

 

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

下面是簡單的使用樣本:

[java] view plaincopy 
  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 電話語音

 

轉載地址:http://disanji.net/2011/05/04/android-layoutinflater-usage/

android動態執行個體化xml布局的3種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.