2016-08-15 10 views
0

나는이 속성은 attrs.xml이 선언 한 :맞춤 속성 (attrs.xml) 값을 얻는 방법은 무엇입니까?

나는 그것을 "# 076B07"해야 값의 얻을 필요가 있지만, 대신에 내가 정수지고있어
<resources> 
    <attr name="customColorPrimary" format="color" value="#076B07"/> 
</resources> 

: "2130771968"

을 저는이 방법으로 값에 접근하고 있습니다 :

int color = R.attr.customColorFontContent; 

이 속성의 실제 가치를 얻는 올바른 방법이 있습니까?

당신에게

답변

2

없음 감사,이 앱 컴파일 할 때 정수 R.attr.customColorFontContent 안드로이드 스튜디오에 의해 생성 된 리소스 식별자이며, 올바른 방법이 아니다.

대신 테마의 속성과 연결된 색상을 가져와야합니다. 이 작업을 수행하기 위해 다음과 같은 클래스를 사용

public class ThemeUtils { 
    private static final int[] TEMP_ARRAY = new int[1]; 

    public static int getThemeAttrColor(Context context, int attr) { 
     TEMP_ARRAY[0] = attr; 
     TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY); 
     try { 
      return a.getColor(0, 0); 
     } finally { 
      a.recycle(); 
     } 
    } 
} 

당신은 다음과 같이 사용할 수 있습니다 :

ThemeUtils.getThemeAttrColor(context, R.attr.customColorFontContent); 
+0

당신을 감사합니다! 너무 많이! – NullPointerException

+0

TR4Android 속성의 기본값을 설정하는 방법을 알고 있습니까? 설정 값 = "# 076B07"이 작동하지 않습니다. – NullPointerException

+0

아무런 문제없이 도와 드릴 수있어서 기쁩니다. – TR4Android

0

다음과 같이 color 속성에 액세스해야합니다

public MyCustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, 0, 0); 
    try { 
     color = ta.getColor(R.styleable.MyCustomView_customColorPrimary, android.R.color.white); //WHITE IS THE DEFAULT COLOR 
    } finally { 
     ta.recycle(); 
    } 

    ... 
}