0

FragmentActivity를 확장하는 클래스가 있습니다. 생성 한 프래그먼트에는 textView 만 있습니다.조각에서 텍스트 뷰의 글꼴을 동적으로 변경합니다.

이 텍스트 뷰의 글꼴 유형을 사용자가 변경할 수있는 액션 바 (이미 완료 됨)에 설정 버튼을 추가하고 싶습니다.

어떻게해야합니까?

FragmentActivity의 프래그먼트 개수가 선험적으로 알려지지 않은 또 다른 문제점이 있습니다 ... 글꼴 유형을 변경하면 모든 프래그먼트가 변경됩니다.

public void setFont(){ 
      TextView textView = (TextView) getView().findViewById(R.id.detailsText); 
      textView.setTypeface(); 
//Another problem how set typeface, because 
//Typeface font = Typeface.createFromAsset(getAssets(),"fonts/font.tff"); couldn't work because I'm inside a Fragment and getAssets() just rise errors.. 
     } 

내가 꽤 붙어있어 .. 내 조각 내부의 방법 changefont을 넣어 시도했습니다,하지만 난 관리 할 수있는 방법을 모르겠어요 .. 너희들이 나를 도울 수 있을까요?

답변

0

Utils.java라는 하나의 클래스 뭔가를 확인하고이 방법을 넣어 : - 이제

public static void setFontSignika_Bold(TextView textView) { 
      Typeface tf = Typeface.createFromAsset(textView.getContext() 
        .getAssets(), "fonts/signikabold.ttf"); 

      textView.setTypeface(tf); 

     } 

을,이 방법으로 전체 응용 프로그램이 사용할 수 있습니다 : -

Utils.setFontSignika_Bold(textView); // Pass your textview object 
+0

는 I는 NullPointerExeption을 얻었다. 그래서이 메서드를 호출 할 때 매개 변수 (TextView) findViewById (R.id.text)를 사용하고 있습니다. 물론 이것은 예외를 발생시킬 것입니다 –

0

또한 만들 수 있습니다 TextView의 서브 클래스를 만들고 내부에 글꼴을 설정하십시오. 컨텍스트 객체에 getAssets를 포함() 메소드 : 확장 된 텍스트 뷰의

예 구현 : 나는 액션 바에서 조각 내부의 텍스트 뷰에 접속을 시도하고 있기 때문에

public class TextViewPlus extends TextView { 
    private static final String TAG = "TextView"; 

    public TextViewPlus(Context context) { 
     super(context); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); 
     String customFont = a.getString(R.styleable.TextViewPlus_customFont); 
     setCustomFont(ctx, customFont); 
     a.recycle(); 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
     tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: "+e.getMessage()); 
      return false; 
     } 

     setTypeface(tf); 
    setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG); 
     return true; 
    } 

} 
+0

내 첫 번째 문제가 아니라, ActionBar에서 Fragment 내부의 TextView에 액세스해야합니다. / –

관련 문제