25

重複の可能性:
アプリケーション全体のフォントを設定することは可能ですか?

アプリ全体にカスタムフォント(.ttf形式)を設定する必要がありますが、どうすればよいですか?可能であれば、マニフェストまたはXMLからが最適なオプションです。

4

5 に答える 5

22

2014年9月編集:

この古いひどい答えをまだ見ている人のために、本当の、良い答えはここにあります:

https://stackoverflow.com/a/16883281/1154026


年:

あなたの最善の策はこれでしょう:

Androidフォントをカスタマイズする

それで

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

「assets」フォルダのルートに.ttfがあります。

于 2012-09-05T12:53:52.417 に答える
11

あなたはこのようにそれを行うことができます。カスタムテキストビューを作成し、それをどこでも使用できます

public class MyTextView extends android.widget.TextView
{

    public MyTextView(Context context)
    {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        if (this.isInEditMode()) return ;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
        final String customFont = a.getString(R.styleable.SomeStyle_font);

        //Build a custom typeface-cache here!
        this.setTypeface(
            Typeface.createFromAsset(context.getAssets(), customFont)
        );  
    }
}

これをattrs.xmlに追加します

<declare-styleable name="SomeStyle">
    <attr name="font" format="string" />
</declare-styleable>

次に、テーマで次のようにします。これにより、すべてのテキストビューでスタイルMyTextViewが使用されるようになります。

<item name="android:textViewStyle">@style/MyTextView</item>

これで、このスタイルのカスタム属性を使用してカスタムフォントを定義できます。

<style name="MyTextView" parent="@android:style/Widget.TextView">
    <item name="font">MyPathToFontInAssets.ttf</item>
</style>

したがって、プロジェクトでMyTextViewを使用するときはいつでも、カスタムフォントが使用されます。

重要:書体はキャッシュされないため、このコードを使用する場合は、すべてのテキストビューでカスタム書体を再利用できるように、カスタム書体キャッシュも作成する必要があります。これにより、アプリケーションが大幅に高速化されます。

更新: Amirが言っていたように、これはカスタムフォントとXMLレイアウト(Android)とほぼ同じですが、 Androidスタイルを使用して、アプリのすべてのテキストビューで自動的に使用します。

于 2012-09-05T13:16:00.860 に答える
10

TextViewサブクラスをセレートし、どこでも使用できます

    public class RobotoTextView extends TextView {

    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RobotoTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            setTypeface(tf);
        }
    }

}

利用方法

<com.test.RobotoTextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

Javaコードでは、TextViewにキャストできます

于 2012-09-05T12:58:47.333 に答える
7
TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Verdana.ttf");

    tv.setTypeface(face);

フォントファイルを/res/のfontsフォルダーに置きます

それが役に立ったら私の答えのように...

于 2012-09-05T12:53:27.603 に答える
0

ここで提案されているようにTextViewを拡張し、それに書体を設定して、アプリ全体で使用できます。

于 2012-09-05T13:04:25.533 に答える