2013-03-01 80 views
5

我想導出Facebook Android SDK作爲我的項目中使用的JAR。如何動態加載R.styleable資源?

這需要動態加載所有資源。

例如,我必須作出改變與此類似:

//findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE); 
int viewID = getResources().getIdentifier("com_facebook_login_activity_progress_bar", "id", getPackageName()); 
findViewById(viewID).setVisibility(View.VISIBLE); 

註釋的線示出了原始的,並且下面的2行示出了變化我提出動態地加載相同的資源。

Facebook SDK聲明瞭一個R.styleable資源,我無法弄清楚如何動態加載它。這裏是原代碼:

private void parseAttributes(AttributeSet attrs) { 
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view); 
    setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM)); 
    isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE); 
    a.recycle(); 
} 

然後在attrs.xml,以下聲明:

<declare-styleable name="com_facebook_profile_picture_view"> 
     <attr name="preset_size"> 
      <!-- Keep in sync with constants in ProfilePictureView --> 
      <enum name="small" value="-2" /> 
      <enum name="normal" value="-3" /> 
      <enum name="large" value="-4" /> 
     </attr> 
     <attr name="is_cropped" format="boolean" /> 
    </declare-styleable> 

如何動態加載這個資源,(如更換R.styleable參考)?

+1

見[訪問<聲明-設置樣式>資源編程](http://stackoverflow.com/questions/13816596/accessing- declare-styleable-resources-programatically)發佈或許可以幫助你解決當前問題 – 2013-03-01 22:46:24

+0

太棒了,感謝您的快速回復。這正是我所需要的。如果您發帖作爲答案,我會選中它! – ch3rryc0ke 2013-03-01 23:08:00

回答

3

我在這裏回答這個問題,因爲任何人都專門試圖將Facebook SDK作爲jar導出。

我用在這個問題的答案所描述的功能: Accessing <declare-styleable> resources programatically

private void parseAttributes(AttributeSet attrs) { 
    int attrArray[] = StyleableHelper.getResourceDeclareStyleableIntArray(getContext(), "com_facebook_profile_picture_view"); 
    //TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view); 
    TypedArray a = getContext().obtainStyledAttributes(attrs, attrArray); 

    setPresetSize(a.getInt(0, CUSTOM)); 
    isCropped = a.getBoolean(1, IS_CROPPED_DEFAULT_VALUE); 
    //setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM)); 
    //isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE); 
    a.recycle(); 
}