2

Android TextViewでフォントタイプを変更するにはどうすればよいですか?

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome!"
    android:textSize="20sp"  />
4

3 に答える 3

6

この属性を使用でき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のロード後にビューを呼び出すことにより、コードでそれを行う必要があります。sansserifmonospaceTypefacesetTypeface()

于 2012-09-23T15:49:34.653 に答える
6

Ted Hoppの回答に追加するには、を参照する場所でTextView、のカスタムフォントを探している場合は、次のコード例を使用します。ActivityTextView

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つを見ることができます。彼らです:

http://www.google.com/webfonts

http://www.fontsquirrel.com/

于 2012-09-23T16:04:35.987 に答える
0

フォントファイル(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);
于 2014-01-24T07:53:03.683 に答える