2016-05-04 17 views
2

の公共タイプフェイス法を作る:私はそれかどうかを知りたい私は今、私は私の<code>MainActivity</code>に<code>onCreate()</code>に次のコードを持って、異なる<code>Activities</code>でカスタムフォントを適用しようとしていますAndroidの

String fontTitle = "fonts/OpenSans-Bold.ttf"; 
Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle); 
page_title.setTypeface(titleFont); 

私は他の活動でそれにアクセスすることができるように公開することが可能です。Typeface

私はFontHelperというクラスを作成しました:

public class FontHelper extends MainActivity { 

    // path for the fonts 
    String fontTitle = "fonts/OpenSans-Bold.ttf"; 

    Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle); 

} 

を私はtextView.setTypeface(FontHelper.titleFont)を使用する場合、他のActivitiesに私はエラーを取得します。このエラーを修正するにはどうすればよいですか?

あなたはこのような各 Activityのためにあなたの Typefaceインスタンスを作成するために、静的なファクトリメソッドを使用することができます
+1

資産フォルダ(使用同じファイル名)にフォントをコピーし、このタグを使用しますか?または、書体を作成する静的なファクトリメソッドですか?あなたがしようとしていることは技術的に可能ですが、メモリリークを作成するために役立つだけで、あなたが知っていることが悪いことであると思います。 –

+0

@XaverKapellerさまざまなアクティビティで書体を使用したいと思っていましたが、重複すると思いましたので、ヘルパークラスを作成することにしました。どのようにしてメモリリークが発生するのですか?私はちょうどそれが私が探していたことをしている最後の例で[この投稿](http://martin.cubeactive.com/android-how-to-use-a-custom-font/)を読んで、これはまだメモリリークを作成する? –

+0

はい、インターネット上のランダムなブログ投稿を盲目的に追跡してはいけません。ヘルパークラスを持つことは悪いことではありませんが、この投稿のヘルパークラスの実装は、書体が作成されたコンテキストをリークするため問題になります。この単純な問題を解決する方法は、タイプフェイスへの参照を保持する静的変数を持たないことです。代わりに、コンテキストをヘルパーメソッドに渡して、各アクティビティの新しいTypefaceインスタンスを作成します。 –

答えて

1

public class FontHelper { 

    private static final String FONT_PATH = "fonts/OpenSans-Bold.ttf"; 

    public static Typeface getCustomTypeFace(Context context) { 
     return Typeface.createFromAsset(context.getAssets(), FONT_PATH); 
    } 
} 

あなたはこのようにそれを使用することができます:

public class ExampleActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final Typeface typeface = FontHelper.getCustomTypeFace(this); 
     ... 
    } 
} 
+0

ありがとう、これは私が探していたものです。皆さんの意見では、これはより良いか、すべてのアクティビティで新しいTypefaceインスタンスを作成していますか? –

+0

これは、すべてのアクティビティで新しいタイプフェイスを作成します。これは、FontHelperクラスの一部としてファクトリで行います。これはユースケースに最適なソリューションです。 –

-1

まずあなたは、共通のアクティビティを作成する必要がありますMasterActivityと呼ばれる

public class MasterActivity extends AppCompatActivity { 

    protected Typeface titleFont; 

    @Override 
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 
     super.onCreate(savedInstanceState, persistentState); 

     // path for the fonts 
     String fontTitle = "fonts/OpenSans-Bold.ttf"; 

     titleFont = Typeface.createFromAsset(getAssets(), fontTitle); 
    } 
} 

MainActivityは次のようにMasterActivityから延び:

public class MainActivity extends MasterActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // set the custom font 
     page_title.setTypeface(titleFont); 
    } 
} 

とAboutActivityがあまりにもMasterActivityから延び

public class AboutActivity extends MasterActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_about); 

     // set the custom font 
     page_title.setTypeface(titleFont); 
    } 
} 
-1

使用このカスタムフォントクラス

public class TextView extends android.widget.TextView { 
    Context mContext; 
    String str; 
    //fonts 
    public static Typeface Font_name; 

    public TextView(Context context) { 
     super(context); 
     mContext=context; 


     initialiseFont(null); 
    } 



    public TextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext=context; 
     TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0); 
     try { 
      str = ta.getString(R.styleable.TextView_font_family); 

     } finally { 
      ta.recycle(); 
     } 
     initialiseFont(str); 
    } 

    public TextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     mContext=context; 
     TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0); 
     try { 
      str = ta.getString(R.styleable.TextView_font_family); 

     } finally { 
      ta.recycle(); 
     } 
     initialiseFont(str); 
    } 

    public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     mContext=context; 
     TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0); 
     try { 
      str = ta.getString(R.styleable.TextView_font_family); 

     } finally { 
      ta.recycle(); 
     } 
     initialiseFont(str); 
    } 
    private void initialiseFont(String font) { 
     if(font==null || font.equals("")){ 

     } 
     else { 
      Font_name = Typeface.createFromAsset(mContext.getAssets(), font); 
      setTypeface(Font_name); 
     } 

    } 
} 

はarrs.xmlにこのタグを追加しますカスタム属性font-familyを読み取る

あなたがのTextViewを使用している場合
<resources> 
    <declare-styleable name="TextView"> 
     <attr name="font_family" format="string"/> 
    </declare-styleable> 
</resources> 

は、すべての活動に新たな書体のインスタンスを作成すると間違って何どこでも

<Your_package_name_which_you_created_custom_font_class.TextView 
     android:text="Hello World!" 
     android:layout_width="wrap_content" 
     app:font_family="OpenSans-Bold.ttf" 
     android:layout_height="wrap_content" /> 
関連する問題