- Home ›
- Android入門 ›
- TextViewクラス ›
- HERE
フォントファミリーとスタイルの設定
広告
表示される文字列に対するフォントファミリーとフォントスタイルの設定方法を確認します。「TextView」クラスで用意されている「setTypeface」メソッドを使います。
setTypeface public void setTypeface(Typeface tf)
Sets the typeface and style in which the text should be displayed. Related XML Attributes: android:typeface android:textStyle Parameters: tf Typefaceクラスのオブジェクト
1番目の引数にフォントファミリーとスタイルを表す「android.graphics.Typeface」クラスのオブジェクトを指定します。詳しくは「Typefaceクラス」を参照して下さい。
具体的には次のように記述します。
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.graphics.Typeface;
public class Test extends Activity {
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView tv = new TextView(this);
tv.setText("Text");
tv.setTypeface(Typeface.DEFAULT_BOLD);
setContentView(tv);
}
}
サンプルプログラム
それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。
package jp.javadrive.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.ViewGroup;
import android.widget.TextView;
import android.graphics.Typeface;
public class Test07_01 extends Activity
{
private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
TextView tv1 = new TextView(this);
tv1.setText("Android");
tv1.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD_ITALIC));
linearLayout.addView(tv1,
new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
TextView tv2 = new TextView(this);
tv2.setText("Android");
tv2.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
linearLayout.addView(tv2,
new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
TextView tv3 = new TextView(this);
tv3.setText("Android");
tv3.setTextScaleX(1.5f);
linearLayout.addView(tv3,
new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
}
}
ビルド後にエミュレーター上で実行します。
( Written by Tatsuo Ikura )
JavaDrive