Android -- LayoutInflater

來源:互聯網
上載者:User

標籤:android   style   c   class   blog   code   

LayoutInflater在Android中是“擴充”的意思,作用類似於findViewById(),不同的是LayoutInflater是用來獲得布局檔案對象的,而 findViewById()是用來獲得具體控制項的。LayoutInflater經常在BaseAdapter的getView方法中用到,用來擷取整個View並返回。

LayoutInflate 的三種用法:

方法一:

LayoutInflater inflater = LayoutInflater.from(this);View layout = inflater.inflate(R.layout.main, null);

方法二:

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

方法三:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.main, null);

簡單樣本:

public class LayoutInflaterActivity extends Activity {    private EditText et;    private Button btn;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 第一種方法        LayoutInflater inflater = LayoutInflater.from(this);        View layout = inflater.inflate(R.layout.main, null);        // 第二種方法        // LayoutInflater inflater = getLayoutInflater();        // View layout = inflater.inflate(R.layout.main, null);        // 第三種方法        // LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);        // View layout = inflater.inflate(R.layout.main, null);        // 擷取具體控制項並執行個體化        et = (EditText) layout.findViewById(R.id.edittext);        et.setBackgroundColor(Color.YELLOW);        btn = (Button) layout.findViewById(R.id.btn);        btn.setBackgroundColor(Color.CYAN);        // 顯示        setContentView(layout);    }}

Android裡面想要建立一個畫面的時候, 一般都是建立一個類, 繼承Activity類, 然後在onCreate裡面使用setContentView方法來載入一個在xml裡定義好的介面.

其實在Activity裡面就使用了LayoutInflater來載入介面, 通過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以獲得一個LayoutInflater, 然後使用inflate方法來載入layout的xml, 對於一個沒有被載入或者想要動態載入的介面, 都需要使用inflate來載入.

對於一個已經載入的介面, 就可以使用這個介面調用findViewById方法來獲得其中的子介面了.

另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據傳入的NAME來取得對應的Object,然後轉換成相應的服務物件。以下介紹系統相應的服務。 

傳入的Name 返回的對象 說明:

WINDOW_SERVICE             WindowManager    管理開啟的視窗程序LAYOUT_INFLATER_SERVICE    LayoutInflater    取得xml裡定義的viewACTIVITY_SERVICE           ActivityManager    管理應用程式的系統狀態POWER_SERVICE              PowerManger    電源的服務ALARM_SERVICE              AlarmManager    鬧鐘的服務NOTIFICATION_SERVICE       NotificationManager    狀態列的服務KEYGUARD_SERVICE           KeyguardManager    鍵盤鎖的服務LOCATION_SERVICE           LocationManager    位置的服務,如GPSSEARCH_SERVICE             SearchManager    搜尋的服務VEBRATOR_SERVICE           Vebrator    手機震動的服務CONNECTIVITY_SERVICE       Connectivity    網路連接的服務WIFI_SERVICE               WifiManager    Wi-Fi服務TELEPHONY_SERVICE          TeleponyManager    電話語音

 

 

 

轉載請註明出處:http://www.cnblogs.com/yydcdut/p/3744877.html

參考:http://hi.baidu.com/laocui172/item/03649bfcaee761c4a835a2ac

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.