1

アプリの書体を変更したいのですが、できません:

AndroidManifest.xml

...
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/CustomTheme"
        >
...

style.xml

....

    <style name="CustomTheme">
        <item name="android:typeface">stheiti.ttf</item>
    </style>
....

しかし、私はエラーが発生します:

エラー: エラー: 文字列型は許可されていません (値が 'stheiti.ttf' の 'android:typeface' で)。

しかし、私は rootAPP/assets/stheiti.ttf を持っています

なぜそのエラーが発生するのかわかりません。多くのサイトで解決策を検索しましたが、同じ問題のあるサイトは見つかりませんでした

前もって感謝します!:=)

4

4 に答える 4

1

次のようにテキストビューでカスタムフォントを設定できます

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

アプリケーション全体にカスタム フォントを実装する場合は、カスタム ビューを作成し、そのビューの初期化時にフォントを設定できます。

public class MyTextView extends TextView {

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

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

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

public void init() {

    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "stheiti.ttf");
    setTypeface(tf ,1);

}
于 2012-06-27T11:03:17.177 に答える
0

アプリケーション全体については、カスタムTextViewを作成する必要があります。xmlのすべてのtextviewについて、以下に示すように、この最初のmakeフォルダーに移動する前に、そのフォントフォルダー内のアセットにフォントの名前を付けてurfontstheiti.ttfを追加します。

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

 public class MyTextView extends TextView{

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

    }

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

    }

    public MyTextView(Context context) {
        super(context);
        init();

    }



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


 }

その後、xml内のすべてのtextviewに対して、xml内のur textviewよりもこのタイプのフォントが必要な場合は、通常のtextviewと同じように必要なtextviewの他のプロパティを追加します。

<YourProjectPackageName.MyTextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22px"
    android:textColor="#34A4c5"
 ></YourProjectPackageName>

アクティビティでは、以下に示すようにカスタムテキストビューを使用します

MyTextView textview=(MyTextView)findViewById(R.id.textview);
于 2012-06-27T11:33:31.507 に答える
0

これを行うことはできません。プログラムですべてのビューにカスタムフォントを設定する必要があります(XMLを介してカスタム書体を設定することはできません。はい、設定できますが、組み込みの書体のみです)

これを確認してください: Android のテーマにカスタム フォントを追加し、Androidでカスタム書体を使用する

于 2012-06-27T11:05:18.203 に答える
0

そのために以下のコードを使用すると、役立つ場合があります。

// Font path
String fontPath = "fonts/trbuc.ttf";

// text view label
TextView txtGhost = (TextView) findViewById(R.id.mTxtViewCustomFont);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);

// Applying font
txtGhost.setTypeface(tf);
于 2012-06-27T11:05:34.143 に答える