Android TextViewでフォントタイプを変更するにはどうすればよいですか?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
Android TextViewでフォントタイプを変更するにはどうすればよいですか?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
この属性を使用できandroid:typeface
ます。例えば:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:typeface="sans"
android:textSize="20sp" />
XML属性では、値、、、、およびのみが許可さnormal
れます。別の(おそらくアプリに同梱されているカスタムのもの)を使用する場合は、TextViewのロード後にビューを呼び出すことにより、コードでそれを行う必要があります。sans
serif
monospace
Typeface
setTypeface()
Ted Hoppの回答に追加するには、を参照する場所でTextView
、のカスタムフォントを探している場合は、次のコード例を使用します。Activity
TextView
Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
txtSampleTxt.setTypeface(blockFonts);
ただし、これを使用する前に、選択したフォントをassets
フォルダにコピーする必要があります。
また、フォントのダウンロードに使用できるWebサイトがいくつかあるSOに関する私の回答の1つを見ることができます。彼らです:
フォントファイル(example.tff)スタイルをAssets / fontsに配置し、独自の文字列をfontpath変数に指定することで、プログラムで目的のフォントを設定できます。
String fontPath="fonts/Unkempt-Regular.ttf";
TextView aboutus=(TextView)findViewById(R.id.aboutus);
Typeface type=Typeface.createFromAsset(getAssets(), fontPath);
aboutus.setTypeface(type);