2010-10-05 14 views

回答

21

我首選的處理方式通脹:

//First get our inflater ready, you'll need the application/activity context for this 
LayoutInflater mInflater; 
mInflater = LayoutInflater.from(mContext); 

//Inflate the view from xml 
View newView = mInflater.inflate(R.layout.my_new_layout, null); 

//Then you'll want to add it to an existing layout object 
mMainLayout.add(newView); 

//Or perhaps just set it as the main view (though this method can also 
// inflate the XML for you if you give it the resource id directly) 
setContentView(newView); 

基本上,你用它在運行時膨脹,現有的XML佈局。通常你會繼續將這些新視圖插入到先前定義的ViewGroups或List對象中。

+0

+1最高分,一個偉大的回答言簡意賅,感謝 – 2011-08-22 19:23:45

+0

@Marloke很有幫助的人 – 2012-07-21 11:23:40

6

不太清楚你的意思,但如果它與膨脹視圖相關,它用來將佈局xml文件加載到你的應用程序中。例如通過

View myWelcome = View.inflate(this, R.layout.welcome, null); 

它更容易,並考慮最佳實踐,讓您查看佈局XML文件中的定義,而不是完全由代碼創建您的意見。

2

佈局充氣用於返回你的完整佈局的java對象

假設你有一個佈局XML文件中的根元素是相對佈局,它包含一個imageview的並且TextView的再利用佈局充氣機可以退貨一個引用整個佈局的視圖對象。

這基本上用於列表視圖和網格視圖,以將它們插入單個行或要重複的元素的佈局對象。

0

你問的使用充氣.. 基本上當你想在一個Java類中使用兩個XML文件,充氣機被使用,它的代碼是在下面給出簡單..

TextView text; 
    View layout; 
     LayoutInflater inflator=getLayoutInflater(); 
     layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null); 

     text=(TextView)layout.findViewById(R.id.text); 
     text.setText("progressing"); 

這裏我用的TextView,這是目前在與ID爲下XML =文本 完蛋了。如果您發現該對得起那麼請這樣的.. 感謝