Android アプリに外部フォント アセットを含めていますが、Eclipse はそれらを xml プレビューでレンダリングできません。
アプリにフォントを含め (/assets/fonts フォルダーに ttf を配置)、コードを使用して textview フォントを動的に設定すると:
mFont = Typeface.createFromAsset(getAssets(),"fonts/fontfilename.ttf");
textview = (TextView) findViewById(R.id.text_view);
textview.setTypeface(mFont);
テキストは、デバイスまたはエミュレーターのアプリのフォントで正しくレンダリングされますが、Eclipse を使用して xml プレビューでレンダリングする方法についての手がかりがありません。私は TextView クラスを拡張しようとしました:
public class ExTextView extends TextView {
private static final Typeface MY_FONT_01 = Typeface.create("mfont-01", 0); //, Typeface.BOLD);
private static final Typeface MY_FONT_02 = Typeface.create("mfont-02", 0); //, Typeface.NORMAL);
public ExTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (getTypeface().equals(MY_FONT_01)) {
Typeface mFont = Typeface.createFromAsset(getContext().getAssets(),"fonts/myfont01.ttf");
setTypeface(mFont);
setPadding(0, (int) (-0.9f * getTextSize()), 0, (int) (-0.2f * getTextSize()));
setTextSize(0x00000001, (int)48);
} else if (getTypeface().equals(MY_FONT_02)) {
Typeface mFont = Typeface.createFromAsset(getContext().getAssets(),"fonts/myfont02.ttf");
setTypeface(mFont);
setPadding(0, (int) (-0.5f * getTextSize()), 0, (int) (-0.5f * getTextSize()));
setTextSize(0x00000001, (int) 24);
}
}
}
これをxmlに入れます:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.extendedtextview.ExTextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="mfont-01"
android:text="ExTextView-01" />
<com.example.extendedtextview.ExTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="mfont-02"
android:text="ExTextView-02" />
</LinearLayout>
しかし、フォントは ExTextView.java で宣言されている内容に従ってレンダリングされません。この非常に単純なアプリではこれが難しい方法であることはわかっていますが、より複雑なシナリオでは、この種の構造が役立つ可能性があるため、どのように機能させるかを考えたいと思います.
アプリを実行しようとすると、logcat に次のエラーが表示されます。
04-03 11:38:08.149: E/AndroidRuntime(689): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.extendedtextview.ExTextView
提案やヒントは大歓迎です。