2012-12-11 29 views
6

Tôi đã một câu hỏi mà tôi không thể tìm thấy bất kỳ sự giúp đỡ:Truy cập <tuyên bố-styleable> nguồn programatically

Có thể, để nhận được tài nguyên-id được giữ bởi một như một int [] programatically không đề cập đến tài nguyên-class R?

<declare-styleable name="com_facebook_login_view"> 
    <attr name="confirm_logout" format="boolean"/> 
    <attr name="fetch_user_info" format="boolean"/> 
    <attr name="login_text" format="string"/> 
    <attr name="logout_text" format="string"/> 
</declare-styleable> 

Vấn đề là tôi không thể giải quyết ID của thuộc tính 'tuyên bố-styleable' định nghĩa - 0x00 luôn được trả về:

int id = context.getResources().getIdentifier("com_facebook_login_view", "declare-styleable", context.getPackageName()); 
int[] resourceIDs = context.getResources().getIntArray(id); 

Bất cứ ý tưởng sẽ được đánh giá rất nhiều! :)

Cảm ơn trước!

Christopher

+2

đó là bởi vì nó là một khai báo-styleable, không phải là một định danh. Bạn đã thử phản chiếu trên lớp R.styleable? – njzk2

+0

Không, tôi đã không đề cập đến điều này - cảm ơn cho gợi ý - Tôi sẽ thử nó bằng cách sử dụng sự phản chiếu :) Vì vậy, không có cách nào để truy cập một tuyên bố theo kiểu động? Tôi sẽ sử dụng nó cho phương thức getContext(). ObtainStyledAttributes (AttributeSet set, int [] attrs); Cảm ơn sự giúp đỡ của bạn! –

+2

Giải quyết nó. Nhưng danh tiếng của tôi quá thấp để trả lời câu hỏi của riêng tôi: ( Tôi sẽ đăng nó trong tám giờ nếu nó không rơi vào quên lãng.) –

Trả lời

15

Đây là giải pháp mang lại nguồn tài nguyên-ID programatically cho đứa trẻ - thẻ định nghĩa cho thẻ:

/********************************************************************************* 
* Returns the resource-IDs for all attributes specified in the 
* given <declare-styleable>-resource tag as an int array. 
* 
* @param context  The current application context. 
* @param name  The name of the <declare-styleable>-resource-tag to pick. 
* @return    All resource-IDs of the child-attributes for the given 
*      <declare-styleable>-resource or <code>null</code> if 
*      this tag could not be found or an error occured. 
*********************************************************************************/ 
public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) 
{ 
    try 
    { 
     //use reflection to access the resource class 
     Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields(); 

     //browse all fields 
     for (Field f : fields2) 
     { 
      //pick matching field 
      if (f.getName().equals(name)) 
      { 
       //return as int array 
       int[] ret = (int[])f.get(null); 
       return ret; 
      } 
     } 
    } 
    catch (Throwable t) 
    { 
    } 

    return null; 
} 

Có lẽ điều này có thể giúp ai đó một ngày. :)

Greetings

Christopher

+0

Và cảm ơn một lần nữa để njzk2 cho trở ngại :) –

+1

Câu trả lời tuyệt vời .. Cảm ơn – Arunkumar

+1

Nó phải là câu trả lời được chấp nhận vì nó cung cấp một giải pháp độc lập 100% R.styleable. –

1

giải pháp Hơi hiệu quả hơn:

public static final int[] getResourceDeclareStyleableIntArray(String name) { 
     Field[] allFields = R.styleable.class.getFields(); 
     for (Field field : allFields) { 
      if (name.equals(field.getName())) { 
       try { 
        return (int[]) field.get(R.styleable.class); 
       } catch (IllegalAccessException ignore) {} 
      } 
     } 

     return null; 
    } 
Các vấn đề liên quan