2014-07-24 6 views
0

Textview에서 확장 한 사용자 정의 클래스가 있으므로 레이아웃의 XML 값을 가져와야합니다. 시도했다XML의 속성 값에서 값 가져 오기

public FontTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setIncludeFontPadding(false); 
    int style = attrs.getAttributeIntValue(com.android.internal.R.styleable.TextAppearance_textStyle,-1); 
    init(style); 
} 

그러나 나는 com.android.internal.R.styleable 패키지가 존재하지 않는다고 말할 수 없다. 나는 밖에서 패키지에 접근 할 수 있다고 생각한다.

xml에서 스타일을 가져올 방법이 있습니까?

styleable.TextAppearance_textStyle의 값은 -2001555입니다.이 값이 변경되거나 항상?를 사용하여 올바른 값을 가져올 수 있습니까?

int style = attrs.getAttributeIntValue(-2001555,-1) 
+1

아니, 그 ID가 내가 속성 세트의 값을 얻는 방법 때문에 컴파일시 – Blackbelt

+0

에 구축 때문에 그것이 응용 프로그램을 중단 할 경우의 99 %에서? –

+0

이 자습서가 도움이되는지 확인하십시오 : http://www.vogella.com/tutorials/AndroidCustomViews/article.html – luiscosta

답변

0

Tyr 이렇게하면 속성 값을 얻을 수 있습니다. 문서에 설명 된대로 TypedArray 색인이 무엇인지주의하십시오.

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

    int[] attrsArray = new int[]{android.R.attr.textStyle}; 
    final TypedArray array = context.obtainStyledAttributes(attrs, attrsArray); 
    int style = array.getInt(0, -1); // returns 1 for bold, 2 for italic 
    array.recycle(); 
}