2011-01-19 55 views
10

我有兩個佈局:main.xml和buttonpanel.xml。在buttonpanel.xml中,在主線性佈局中,我將重力設置爲底部。現在我試圖使用下面的代碼添加buttonpanel佈局。如何使用layoutinflator在運行時添加視圖?

setContentView(R.layout.main); 
LinearLayout layout=(LinearLayout)findViewById(R.id.mainlinearlayout); 
LayoutInflater inflater= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view=inflater.inflate(R.layout.buttonpanel,null); 
layout.addView(view); 

我的問題是面板被添加到頂部,但我已經在buttonpanel.xml中將重力設置爲底部。如果我使用include將buttonpanel.xml添加到main.xml,它工作正常。

任何人都可以幫助我什麼是我的代碼有問題嗎?

+0

[使LayoutInflater有意義](http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater) – 2012-07-03 12:49:25

+0

請檢查以獲得更詳細的解釋:http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater – andig 2011-02-17 09:16:46

回答

17

我有問題的佈局參數被刪除時,你正在做的方式充氣的意見。如果我使用一個稍微不同的呼叫inflate我的佈局參數都得到尊重:

parent_view = inflater.inflate(R.layout.buttonpanel, parent); 

或者對於我來說,當父母不支持添加視圖到它:

view = inflater.inflate(R.layout.buttonpanel, parent, false); 

也許這將解決您的問題也是如此。

編輯:根據給定的參數返回不同的視圖。 LayoutInflater

0

您可以使用childview索引來設置它。我認爲這將有助於you.try另一種方法爲addview,其中包括兒童索引

相關問題