inflate與findViewById

總結的來說,inflate方法獲得佈局,findViewById方法獲得佈局下的控件




在活動類中,直接使用findViewById(),是獲得指定佈局(setContentView()設置的佈局)下的控件



在非活動類中使用inflate方法獲得佈局

先獲取LayoutInflater對象:

LayoutInflater mInflater=LayoutInflater.from(this);//LayoutInflater.from(getContext());


bView=mInflater.inflate(R.layout.XX_item,null);


或者直接

bView=LayoutInflater.from(this).inflate(R.layout.XX_item,null);

得到佈局,我們就可以用bView.findViewById()得到相應佈局文件裏的某些控件。

例如:

TextView textView=(TextView) bView.findViewById(R.id.text_View);

textView.setText......



發佈了8 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章