TextViewで独自Fontを設定する方法
TextViewではTypefaceを使用してBoldやItalicなどに変更できると思いますが、独自のFontを使用する方法を書いてみたいと思います。
方法はいたって簡単です。まずFontのファイルを用意します。今回は下記2つのファイルを用意しました。
UnrealTournament.ttf
Plok.ttf
このファイルをassetsディレクトリ配下に配置します。
layoutのxmlです。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="Hello Android!"/> <TextView android:id="@+id/main_textView_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="Hello Android!"/> <TextView android:id="@+id/main_textView_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="Hello Android!"/> </LinearLayout>
TextViewを3つ並べてるだけです。
Activityクラスです。
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView text1 = (TextView)findViewById(R.id.main_textView_1); text1.setTypeface(Typeface.createFromAsset(getAssets(), "UnrealTournament.ttf")); TextView text2 = (TextView)findViewById(R.id.main_textView_2); text2.setTypeface(Typeface.createFromAsset(getAssets(), "Plok.ttf")); } }
setTypefaceでそれぞれFontを設定してます。
実行するとこんな感じです。
簡単ですね。
TextViewと言いましたが、ほかのViewでも同様に設定できます。
今回使用した、Fontのファイルは以下のサイトで配布されています。
UnrealTournament.ttf
http://www.dafont.com/unreal-tournament.font