- Home ›
- Android入門 ›
- EditTextクラス ›
- HERE
文字サイズの設定
広告
表示される文字列の文字のサイズを設定する方法を確認します。「EditText」クラスの親クラスである「TextView」クラスで用意されている「setTextSize」メソッドを使います。
setTextSize public void setTextSize(float size)
Set the default text size to the given value, interpreted as "scaled pixel" units. This size is adjusted based on the current density and user font size preference. Related XML Attributes: android:textSize Parameters: size The scaled pixel size.
1番目の引数に文字サイズをfloat型の値で指定します。単位はピクセルです。
具体的には次のように記述します。
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); EditText edit = new EditText(this); edit.setText("", BufferType.NORMAL); edit.setTextSize(12.0f); setContentView(edit); }
サンプルプログラム
それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。
package jp.javadrive.android; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView.BufferType; public class Test10_01 extends Activity { private final int FP = ViewGroup.LayoutParams.FILL_PARENT; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); setContentView(linearLayout); EditText edit1 = new EditText(this); edit1.setWidth(200); edit1.setText("Yamada", BufferType.NORMAL); edit1.setTextSize(12.0f); linearLayout.addView(edit1, createParam(WC, WC)); EditText edit2 = new EditText(this); edit2.setWidth(200); edit2.setText("Katou", BufferType.NORMAL); edit2.setTextSize(24.0f); linearLayout.addView(edit2, createParam(WC, WC)); } private LinearLayout.LayoutParams createParam(int w, int h){ return new LinearLayout.LayoutParams(w, h); } }
ビルド後にエミュレーター上で実行します。
2つのテキストボックスが表示され、それぞれのテキストボックスの文字サイズが設定された値になっています。
( Written by Tatsuo Ikura )