2012-01-09 24 views
43

Androidのテーマにカスタムフォントを追加する方法はありますか?Androidのテーマにカスタムフォントを追加する

私はQuick Tip: Customize Android Fontsと読みましたが、ここではカスタムフォントをテキストに追加する必要があります。

TextView txt = (TextView) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf"); 
txt.setTypeface(font); 

しかし、私はカスタムフォントをスタイル/テーマで設定したいと思います。

答えて

-1

私はこれがあなたの意図したものだと願っていますが、そうでない場合は、とにかく他の人にとっては良い参考になるはずです。

**注:フォントを見つけることができるコンピュータ/ローカルディスク(C:)/ Windowsの/フォント**

コピー新しくに上記のフォントフォルダに使用し、それをペーストしたいフォントEclipseのassetsフォルダに作成されたフォルダ。

private void initTypeFace() 
    { 
     TypeFace tf = TypeFace.createFromAsset(getAsset(), 
         "Chantelli Antiqua.ttf"); 

     TextView txt = (TextView) findViewById(R.id.custom_font); 

     txt.setTypeface(tf); 
     example_button1.setTypeface(tf); 
     example_button2.setTypeface(tf); 
    } 
0

私はあなたのテーマに含めることができる追加のXMLパラメータを使用してTextViewをカスタム化すると思います。

コンストラクタTextView(コンテキストコンテキスト、AttributeSet attrs)でこの値を解析して、この値を初期化することができます。ビューのカスタムattrsを定義して初期化するなどのリンクを確認してください(this)。

12

これはthis questionthis oneと重複していると思います。

FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets()); 

XMLで:だから、理論的にはあなたがスタイルを作成することができますし、FontUtils /ランタイムコードと一緒にそれを使用

 <TextView 
      android:id="@+id/my_label" 
      android:tag="condensed" 
      android:text="@string/label" 
      ... /> 

ランタイムでの私の活動において

は、私はこのようなものを使用します。

<style name="roboto_condensed"> 
    <item name="android:tag">condensed,your-own-css-like-language-here</item> 
</style> 

FontUtilsクラス:

public class FontUtils { 
    private static Typeface normal; 

    private static Typeface bold; 

    private static Typeface condensed; 

    private static Typeface light; 

    private static void processsViewGroup(ViewGroup v, final int len) { 

    for (int i = 0; i < len; i++) { 
     final View c = v.getChildAt(i); 
     if (c instanceof TextView) { 
     setCustomFont((TextView) c); 
     } else if (c instanceof ViewGroup) { 
     setCustomFont((ViewGroup) c); 
     } 
    } 
    } 

    private static void setCustomFont(TextView c) { 
    Object tag = c.getTag(); 
    if (tag instanceof String) { 
     if (((String) tag).contains("bold")) { 
     c.setTypeface(bold); 
     return; 
     } 
     if (((String) tag).contains("condensed")) { 
     c.setTypeface(condensed); 
     return; 
     } 
     if (((String) tag).contains("light")) { 
     c.setTypeface(light); 
     return; 
     } 
    } 
    c.setTypeface(normal); 
    } 

    public static void setCustomFont(View topView, AssetManager assetsManager) { 
    if (normal == null || bold == null || condensed == null || light == null) { 
     normal = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Regular.ttf"); 
     bold = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Bold.ttf"); 
     condensed = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Condensed.ttf"); 
     light = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Light.ttf"); 
    } 

    if (topView instanceof ViewGroup) { 
     setCustomFont((ViewGroup) topView); 
    } else if (topView instanceof TextView) { 
     setCustomFont((TextView) topView); 
    } 
    } 

    private static void setCustomFont(ViewGroup v) { 
    final int len = v.getChildCount(); 
    processsViewGroup(v, len); 
    } 
} 
3

あなたは資産フォルダにカスタムフォントの種類を含めて、そこからそれを盗んことができます。

として書体を宣言します、最後

helveticaBold = Typeface.createFromAsset(getAssets(), "helvetica_bold.ttf"); 
helveticaRegular = Typeface.createFromAsset(getAssets(), "helvetica_regular.ttf"); 

としてのTextView又はのEditTextのテキストの書体を設定します:()次のコードを書くのonCreateで

Typeface helveticaBold; 
Typeface helveticaRegular; 

editText.setTypeface(helveticaRegular); 

それだけです...

+0

はい、彼は何を求めているのではありません – user1010160

4

私のCustomTextViewを使用すると、assetsフォルダー内のフォントファイル名をXMLレイアウトファイルに直接指定することができます。

私の答えはhere

19

残念ながら、Androidのは、あなたのアプリケーション全体のフォントを変更するために探している、簡単迅速かつクリーンな方法を提供していませんされています。しかし、最近私はこの問題を調べ、コーディングせずにフォントを変更できるいくつかのツールを作成しました(xml、スタイル、およびテキストの外観でもすべてを行うことができます)。これらのソリューションは、他の回答と同様のソリューションに基づいていますが、はるかに柔軟性があります。 this blogですべてのことを読むことができ、githubプロジェクトhereを参照してください。

これらのツールの適用例を以下に示します。すべてのフォントファイルをassets/fonts/に入れてください。次に、これらのフォントをxmlファイル(たとえばres/xml/fonts.xml)で宣言し、このファイルをアプリの早めにTypefaceManager.initialize(this, R.xml.fonts);(たとえば、アプリケーションクラスのonCreate)に読み込みます。あなたのxmlレイアウトでカスタムのTextView com.innovattic.font.FontTextViewにflFont属性を設定することにより、

<?xml version="1.0" encoding="utf-8"?> 
<familyset> 

    <!-- Some Font. Can be referenced with 'someFont' or 'aspergit' --> 
    <family> 
     <nameset> 
      <name>aspergit</name> 
      <name>someFont</name> 
     </nameset> 
     <fileset> 
      <file>Aspergit.ttf</file> 
      <file>Aspergit Bold.ttf</file> 
      <file>Aspergit Italic.ttf</file> 
      <file>Aspergit Bold Italic.ttf</file> 
     </fileset> 
    </family> 

    <!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' --> 
    <family> 
     <nameset> 
      <name>bodoni</name> 
      <name>anotherFont</name> 
     </nameset> 
     <fileset> 
      <file>BodoniFLF-Roman.ttf</file> 
      <file>BodoniFLF-Bold.ttf</file> 
     </fileset> 
    </family> 

</familyset> 

今、あなたは(あなたが私は上記のツールを使用して)自分のスタイルやXMLでこれらのフォントを使用することができます。xmlファイルは次のようになります。以下は、あなただけのres/values/styles.xmlを編集することにより、あなたはあなたの全体のアプリですべてのテキストにフォントを適用することができますどのように見ることができます。

伴う res/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> 

    <!-- Application theme --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <item name="android:textViewStyle">@style/MyTextViewStyle</item> 
    </style> 

    <!-- Style to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView"> 
     <item name="android:textAppearance">@style/MyTextAppearance</item> 
    </style> 

    <!-- Text appearance to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo"> 
     <!-- Alternatively, reference this font with the name "aspergit" --> 
     <!-- Note that only our own TextView's will use the font attribute --> 
     <item name="flFont">someFont</item> 
     <item name="android:textStyle">bold|italic</item> 
    </style> 

    <!-- Alternative style, maybe for some other widget --> 
    <style name="StylishFont"> 
     <item name="flFont">anotherFont</item> 
     <item name="android:textStyle">normal</item> 
    </style> 

</resources> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <!-- This text view is styled with the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This uses my font in bold italic style" /> 

    <!-- This text view is styled here and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:flFont="anotherFont" 
     android:textStyle="normal" 
     android:text="This uses another font in normal style" /> 

    <!-- This text view is styled with a style and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     style="@style/StylishFont" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This also uses another font in normal style" /> 

</LinearLayout> 

は、テーマを適用することを忘れないでくださいAndroidマニフェストの

+0

ありがとう!偉大な記事.. –

+0

あなたもJavaコードを使用しているので、もっと簡単な方法でそれをやり遂げることができます –

+0

これは本当にアンドロイドでネイティブであるべきです – Zach

関連する問題