2011-08-02 2 views
3

TextView의 글꼴을 LinearLayout의 반복을 통해 반복하고 instanceof을 사용하여 글꼴을 설정하려고합니다.안드로이드 instanceof 모든 위젯을 감지?

양식은 현재 4 TextView '과 하나의 Button으로 구성됩니다.

아래의 코드는 모두 Views입니다. ButtonTextView입니까? 나는 각 뷰의 클래스 이름을 기록하면

/* Set fonts */ 
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper); 
    for (int i = 0; i < ll.getChildCount(); i++) {   
     View v = ll.getChildAt(i);   
     if (v instanceof TextView) { 
      ((TextView) v).setTypeface(Fonts.get3dDumbFont(this)); 
     } 
    } 

IT는 텍스트 뷰를 반환하고 버튼 그래서 정확한 컨트롤이 포착되고있다 알고있다.

문제는 Button 및 TextView의 글꼴이 설정되고 있으며 TextView의 글꼴 만 필요합니다.

나는 다음과 같은 일을하고 있지만 위의 코드가 예상대로 작동하지 않는 이유에 대해 흥미를 느끼고 있습니다.

/* Set fonts */ 
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper); 
    for (int i = 0; i < ll.getChildCount(); i++) {   
     View v = ll.getChildAt(i);   
     if (v.getClass().getName().contains("TextView")) { 
      ((TextView) v).setTypeface(Fonts.get3dDumbFont(this)); 
     } 
    } 

Button과 TextView가 모두 View 유형입니까? 그렇다면이 작업을 수행하는 올바른 방법은 무엇입니까?

도움을 주시면 감사하겠습니다. 리키.

답변

7

실제로 Button은 TextView의 하위 클래스입니다! 이것이 TextView (텍스트 뷰)이기도합니다. instanceof이 버튼을 제외하거나

if (v.getClass() == TextView.class) 

를 사용하지만 (당신이 그들을 사용하는 경우)이 텍스트 뷰의 다른 서브 클래스 일치하지 않습니다하는 경우

http://developer.android.com/reference/android/widget/Button.html

public class Button extends TextView 

당신은 두 번째를 만들 수 있습니다.

+0

에크 TextView Class

확인을 확장 간단하다. 바보 나. 감사. – Ricky

관련 문제