2013-07-25 128 views
-3

我有一個共同的LinearLayout,我想將其放入其自己的佈局文件。它將用於在不同的佈局文件中環繞不同的視圖。是否有可能在視圖中包裝一個包含(或實現類似效果)?包裝包括視圖

+0

LinearLayout究竟有什麼不同?它是以某種方式設計的嗎? – Karakuri

+0

@Karakuri最重要的是我想在自己的文件中有一個自定義佈局,然後用它來環繞不同的視圖,但對於include來說似乎不可能。 – btse

+0

我不認爲有重新使用父佈局的'wrap'功能。 –

回答

1

I have a common LinearLayout that I want to put into its own layout file

放置LinearLayout在一個單獨的佈局文件。膨脹此佈局文件:

View mLinearLayoutView = getLayoutInflater().inflate(R.layout.linear_layout, null); 

LinearLayout ll = (LinearLayout) mLinearLayoutView.findViewById(R.id.my_linear_layout); 

// Inflate the view you want to include within the LinearLayout 
View mChildView = getLayoutInflater().inflate(R.layout.any_other_child_view, null); 

// Initialize/setup any child components 

ll.addView(mChildView);  // Or, ll.addView(mChildView, optional_layout_parameters); 

setContentView(mLinearLayoutView);