2010-02-25 79 views
185

我有用XML定義的佈局。它還包含:如何使佈局膨脹一個視圖

<RelativeLayout 
    android:id="@+id/item" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

我想膨脹這個RelativeView與其他XML佈局文件。根據情況我可能會使用不同的佈局。我應該怎麼做?我正在嘗試不同的變化

RelativeLayout item = (RelativeLayout) findViewById(R.id.item); 
item.inflate(...) 

但他們都沒有工作正常。

回答

327

我不知道我按照您的question-你試圖連接一個子視圖到RelativeLayout的?如果是這樣,你想要做的線沿線的東西:

RelativeLayout item = (RelativeLayout)findViewById(R.id.item); 
View child = getLayoutInflater().inflate(R.layout.child, null); 
item.addView(child); 
+3

我得到一個錯誤:02-25 22:21:19.324:錯誤/ AndroidRuntime(865):引起:java.lang.IllegalStateException:指定的子項已經有父項,必須調用子項的removeView父親第一 – 2010-02-25 21:24:45

+0

@MichalDymel:你可能正在膨脹相同的xml agian。應該膨脹另一個。 – 2011-11-04 10:35:08

+24

現在就試試這個,我需要:View child = getLayoutInflater()。inflate(R.layout.child,null); – James 2012-05-24 00:23:34

93

您膨脹一個XML資源。請參閱LayoutInflater doc

如果你的佈局是在mylayout.xml,你會做這樣的事情:

View view; 
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null); 

RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item); 
+4

我有我的主視圖膨脹。問題是我想從其他XML文件中膨脹一部分。這個項目RelativeLayout是更大的佈局的一部分,我想用另一個XML文件的佈局來填充它。 – 2010-02-25 16:58:28

+0

嗡嗡聲我對不起,然後我不知道:(你在你的主視圖中使用類似於的東西來包含其他佈局嗎? – ccheneson 2010-02-25 17:13:37

+0

不,這是簡單的佈局,我想我會只用編程方式添加我的元素 - 沒有XML。幫助! – 2010-02-25 21:25:39

16

這是有幫助添加到這一點,即使它是一個老帖子,如果正從XML充氣子視圖將被添加到viewgroup佈局,你需要調用膨脹與線索它將被添加到什麼類型的視圖組。像:

View child = getLayoutInflater().inflate(R.layout.child, item, false); 

充氣方法是相當重載,並介紹了文檔中的這部分用法。我遇到了一個問題,那就是在我進行這種類型的更改之前,xml中的單個視圖沒有正確對齊父項。

+0

最後,謝謝:)這允許將多個視圖添加到單個父級,但無需擔心從XML中讀取的LayoutParams。做得好! – Sabo 2013-09-14 20:57:21

20

雖然晚了答案, 但想補充一點,一個辦法讓這個

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(R.layout.mylayout, item); 

其中item是要增加一個子佈局的父佈局。

+0

(甚至更晚更晚)我很難實現你的答案。我嘗試了上面提到的代碼,但是用'R.layout.activity_layout'代替'item' 我得到了「沒有適用的方法來'(int,int)'」我可以爲你的想法困擾你嗎? – Kyle 2012-10-31 21:46:45

+1

@Kyle - 甚至更晚。你不能使用'R'資源作爲對象的引用,它們只是'int's。你必須使用'findViewById'來傳遞'R'資源,然後將其轉換爲你想要的對象。然後你可以在'inflate'這樣的函數調用中使用它。 (即''ViewGroup item =(ViewGroup)findViewById(R.layout.activity_layout);'...那麼你可以使用'item'像上面那樣。) – 2013-01-27 05:56:55

+0

我想我會使用'RelativeLayout'而不是'ViewGroup'在我上面的例子中,因爲你不能實例化基類'ViewGroup'。 5分鐘的編輯規則給了我。大聲笑 – 2013-01-27 06:05:13

5

如果你在一個活動是不是你可以使用靜態from()方法從LayoutInflater類來獲得一個LayoutInflater,或從上下文方法getSystemService()太請求服務:

LayoutInflater i; 
Context x;  //Assuming here that x is a valid context, not null 

i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//OR 
i = LayoutInflater.from(x); 

(我知道這是近4年前,但仍然值得一提)

2

如果你想添加一個單一視圖多個時間,那麼你必須使用

layoutInflaterForButton = getActivity().getLayoutInflater(); 

for (int noOfButton = 0; noOfButton < 5; noOfButton++) { 
     FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null); 
     btnContainer.addView(btnView); 
    } 

如果你喜歡

layoutInflaterForButton = getActivity().getLayoutInflater(); 
    FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null); 

for (int noOfButton = 0; noOfButton < 5; noOfButton++) { 
      btnContainer.addView(btnView); 
     } 

那麼它會拋出一切準備添加的視圖的例外。

8

更簡單的方法是使用

View child = View.inflate(context, R.layout.child, null) 
item.addChild(child) //attach to your item 
7

佈局通脹

View view = null; 
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null); 
main.addView(view); 
1

我有困難的時候與這個錯誤,因爲我的特殊情況,但最後還是找到了解決辦法。

我的情況:我使用單獨的視圖(XML),它包含WebView,然後在我的主活動視圖中單擊一個按鈕時在AlertDialog中打開。但不知何故WebView屬於主要活動視圖(可能是因爲我從這裏拉的資源),所以就在我把它分配給我的AlertDialog(作爲一個視圖)之前,我必須得到我的WebView的父母,把它轉換爲ViewGroup,然後刪除ViewGroup上的所有視圖。這工作,我的錯誤消失了。

// set up Alert Dialog box 
AlertDialog.Builder alert = new AlertDialog.Builder(this); 
// inflate other xml where WebView is 
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService 
       (Context.LAYOUT_INFLATER_SERVICE); 
View v = layoutInflater.inflate(R.layout.your_webview_layout, null); 
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id); 

// more code... 

....我裝後來經過我的WebView ....

// first, remove the parent of WebView from it's old parent so can be assigned a new one. 
ViewGroup vg = (ViewGroup) webView.getParent(); 
vg.removeAllViews(); 

// put WebView in Dialog box 
alert.setView(webView); 
alert.show(); 
-1

我下面的代碼片段用於此,它爲我工作。

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item); 
View child = getLayoutInflater().inflate(R.layout.child, null); 
linearLayout.addView(child);