2016-07-29 60 views
0

我有一個Android安裝程序,該安裝程序具有一個資源文件,該資源文件引用來自兩個選項中的單個佈局。確定兩個資源ID是否指向相同的佈局

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<resources> 
    <item name="another_resource_id" type="layout">@layout/some_layout</item> 
</resources> 

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<resources> 
    <item name="another_resource_id" type="layout">@layout/some_layout2</item> 
</resources> 

這將產生R.layout

public static final class layout { 
    ... 
    public static final int another_resource_id=0x7f030000; 
    public static final int some_layout=0x7f030001; 
    public static final int some_layout2=0x7f030002; 
    ... 
} 

所以,問題是,從代碼中,我希望能夠告訴它佈局another_resource_id資源標識符是指。

+0

也許'Resources.getResourceName()'幫助 –

+0

@Jiang YD這隻會返回some_layout或another_resource_id取決於所使用的渣油。 –

回答

0

我會回答我的問題:似乎沒有直接的方法來告訴佈局的根視圖,除了挖掘並獲取佈局的原始XML。比較這兩個是我如何能夠判斷他們是否是相同的觀點。很長的路,但它是。以下代碼僅比較底層元素的資源ID的添加。很顯然,你可以把很多更復雜的邏輯。

try { 
     XmlResourceParser xrp = ctx.getResources().getLayout(id); 
     int eventType; 
     xrp.next(); 

     String resourceIds = ""; 
     do { 
      eventType = xrp.nextToken(); 
      if (eventType == XmlPullParser.START_TAG) { 
       int attrs = xrp.getAttributeCount(); 
       if (attrs > 0) { 
        for (int i = 0; i < attrs; i++) { 
         if (xrp.getAttributeName(i).compareTo("id") == 0) { 
          resourceIds += xrp.getAttributeValue(i); 
         } 
        } 
       } 
      } 
     } while (eventType != XmlPullParser.END_DOCUMENT); 

     returnVal = resourceIds.hashCode(); 

    } catch (XmlPullParserException e) { 
      //   e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
      //   e.printStackTrace(); 
    } catch (IOException e) { 
      //  e.printStackTrace(); 
    } catch (Resources.NotFoundException e) { 
      //  e.printStackTrace(); 
    } 
相關問題