2012-04-10 44 views
1

我在res/xml /文件夾中有一個XML索引,我希望它包含其他xml文件,以便在解析R.xml.index時,所有文件都合併成一個資源。Android在Eclipse中合併/包含嵌套的XML非佈局資源

我試圖include layout trick適應XML解析,所以我index.xml看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<Index xmlns:android="http://schemas.android.com/apk/res/android"> 
<Sheet> 
    <include xml="o_2sq_m.xml"/> 
    <include xml="o_2sq_r.xml"/> 
</Sheet> 
<Sheet> 
    <include xml="o_sq_tr_m.xml"/> 
    <include xml="o_sq_tr_r.xml"/> 
</Sheet> 
</Index> 

和文件o_2sq_m.xml,這是在同一文件夾中INDEX.XML,看起來像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<Challenge xmlns:android="http://schemas.android.com/apk/res/android"> 
<Dimensions maxX="512" maxY="342" /> 
<Point id="1" x="94" y="101" color="0x00000000" /> 
... 
</Challenge> 

但是,當我與分析INDEX.XML和XmlPullParser,我看到它解析包括標籤,而不需要展開調試,即它不訪問文件的樹o_2sq_m.xml

我應該怎麼做才能使android將文件包含在另一個文件中?

回答

1

如果你沒有做太多的XML進口(如在創建時間),你可以使用getResources().getIdentifier(),這對於這樣一個指數(除去.xml的屬性)

<Index xmlns:android="http://schemas.android.com/apk/res/android"> 
<Sheet> 
    <include customAttr="o_2sq_m"/> ... 
</Sheet> 

,並考慮到您要包含的文件被命名爲o_2sq_m.xml,您可以使用下面的代碼:

switch(tag) { 
case "include" : 
    String xmlid = xpp.getAttributeValue(null, "customAttr"); 
    Int xmlIncludedId = res.getIdentifier(xmlid, "xml", getPackageName()); 
    if(xmlIncludedId != 0) { 
    // Here the xmlIncludedId can be used to import other XML files. 
    // E.g. getResources().getXml(xmlIncludedId) returns an XmlResourceParser 
    } 
+0

謝謝,這是我一直在尋找的東西。 – 2012-04-11 10:27:35

0

該標籤用於佈局,不適用於任何xml文件。如果您將其用於佈局,佈局充氣器將在需要時將其替換爲相應的文件。在一個普通的xml中,它將被認爲是一個像其他標籤一樣的標籤。我不認爲有可能做你想做的事。