2013-02-14 8 views
0

그래서 기본적으로 XML 레이아웃의 모든보기에서 글꼴을 변경하려고합니다. 이는 비 xml 접근 방식을 통해서만 수행 할 수 있습니다. 어떤 이유인지, 비정상 인 뷰 그룹 하위에서는 setFont가 작동하지 않습니다. 나는 viewGroup에 대해 잘못 생각하고 있다고 생각합니다 ... 어떻게하면 더 잘 인스턴스화 할 수 있습니까? 이 코드는 버튼과 같은 일반 뷰를 사용할 때 작동하지만, 버튼과 텍스트 뷰를 백만 개로 정의하면 레이아웃에서 뷰 그룹이 만들어지고 폰트를 변경하기위한 모든 뷰가 반복됩니다. 어서 도와주세요!팽창 된 viewGroup이 작동하지 않는 이유는 무엇입니까?

public static void applyFonts(final View v, Typeface fontToSet) { 
    try { 

     if (v instanceof ViewGroup) { 
      ViewGroup vg = (ViewGroup) v; 

      for (int i = 0; i < vg.getChildCount(); i++) { 
       View child = vg.getChildAt(i); 
       applyFonts(child, fontToSet); 
      } 
     } else if (v instanceof TextView) { 
      ((TextView) v).setTypeface(fontToSet); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null); 


    Typeface rockwellFont = Typeface.createFromAsset(getAssets(), "Rockwell.ttf"); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null); 
    applyFonts(vg,rockwellFont); 
    setContentView(vg); 
+0

검사처럼 할 수있는이

package com.shahidawat.external; import com.shahidawat.R; import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); init(context); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { Typeface type = Typeface.createFromAsset(context.getAssets(), "fonts/MonotypeCorsiva.ttf"); setTypeface(type); setTextColor(context.getResources().getColor(R.color.black)); } } 

처럼 을 사용자 정의 글꼴이 원하는보기를 확장 .. HTTP : //stackoverflow.com/questions/7484471/loop-through-all-childs-of-groupview –

+0

어댑터가 없습니까? 매우 쉽게 할 수있는 어댑터 –

+0

실제로 'Activity'에서 * vg *를 콘텐츠보기로 사용 하시겠습니까, 아니면 콘텐츠보기의 일부로 사용 하시겠습니까? 아니면 그냥 ** 부 풀리시겠습니까? – Luksprog

답변

1

그것의 더 나은 당신이 다음 XML 당신이이

<com.shahidawat.external.CustomTextView 
     android:id="@+id/txtHeader" 
     android:layout_width="match_parent" 
     android:layout_height="30dp" 
     android:layout_marginBottom="5dp" 
     android:background="@drawable/separater" 
     android:gravity="bottom|center_horizontal" 
     android:text="Header" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 
관련 문제