android LayoutInflater.inflate()的參數及其用法

來源:互聯網
上載者:User

標籤:android   blog   http   檔案   art   cti   

內容來自:http://blog.csdn.net/lovexieyuan520/article/details/9036673

很多人在網上問LayoutInflater類的用法,以及inflate()方法參數的含義,現解釋如下:

inflate()的作用就是將一個用xml定義的布局檔案尋找出來,注意與findViewById()的區別,inflate是載入一個布局檔案,而findViewById則是從布局檔案中尋找一個控制項。

 

1.擷取LayoutInflater對象有三種方法

LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

 

2.關於LayoutInflater類inflate(int resource, ViewGroup root, boolean attachToRoot)方法三個參數的含義

resource:需要載入布局檔案的id,意思是需要將這個布局檔案中載入到Activity中來操作。

root:需要附加到resource資源檔的根控制項,什麼意思呢,就是inflate()會返回一個View對象,如果第三個參數attachToRoot為true,就將這個root作為根對象返回,否則僅僅將這個root對象的LayoutParams屬性附加到resource對象的根布局對象上,也就是布局檔案resource的最外層的View上,比如是一個LinearLayout或者其它的Layout對象。

attachToRoot:是否將root附加到布局檔案的根視圖上

這就是我的理解,如有不明白或者有誤,請廣大網友給我留言,共同進步!

聯繫我們

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