''[[MobileApp>Mobile Application Guide]]'' > ''[[Android>Introduction to Android]]'' > ''[[GUI>Android GUI]]'' > ''文字'' ''[[MobileApp>Mobile Application Guide]]'' > ''[[Android>Introduction to Android]]'' > ''[[GUI>Android GUI]]'' > ''Text display'' ~ ~ *Text display [#j81af56a] ~ 以下2つの文字描画方法を記述~ Ⅰ.Text View を使用する方法 ・・・ 用意されたWidgetの部品を利用して文字を配置~ Ⅱ.キャンパス上に 文字列を直接描画する方法 ・・・ グラフィックスとして文字を描画~ ~ ** Ⅰ.TextViewで描画 [#r0182c59] ・TextView に setText() をすることで文字表示~ ・「setText("Hello")」 とするだけ。 ~ ***Text View の属性 [#x81ec76b] [[TextViewの属性>Android TextViewの属性]] ~ ***TextView のソース例 [#vfe70546] ・onCreate()で描画する例~ ・onDraw()で描画する必要は無い~ ・スクロールはできない。ScrollViewを使う。 import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.view.ViewGroup.LayoutParams; public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView test = new TextView(this); test.setText("Hello World"); setContentView(test); } } 参考 : [[JavaDriveさんのページ>http://www.javadrive.jp/android/textview/index2.html]]~ 参考 : [[雑記帳さんのページ>http://d.hatena.ne.jp/androidprogram/20100514/1273841754]]~ 参考 : [[Android奔走記 さん>http://weide-dev.blogspot.com/2010/03/textview.html]]~ ~ ** Ⅱ.Canvasに文字列描画 [#ke34c4ef] ・Canvasの場合、Viewは自分で作成する~ ・onDraw()時に canvas に drawText()する *** ソース [#mfb3b830] package com.chinsan.string2; import android.app.Activity; import android.os.Bundle; import android.view.Window; //文字列の表示 public class String2 extends Activity { //初期化 @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new StringView(this)); } } package com.chinsan.string; import android.content.*; import android.graphics.*; import android.view.*; //文字列の表示 public class StringView extends View { //コンストラクタ public StringView(Context context) { super(context); setBackgroundColor(Color.WHITE); } //描画 @Override protected void onDraw(Canvas canvas) { //描画オブジェクトの生成 Paint paint=new Paint(); paint.setAntiAlias(true); //文字描画 paint.setTextSize(12); paint.setColor(0xFF000000); canvas.drawText("画面サイズ:"+getWidth()+"x"+getHeight(),0,30,paint); } } ~ ~ **その他 [#h830ea99] ***文字の大きさ [#a1f39ebc] 文字の大きさは単位SPでのセットが推奨されている。(解像度に応じて伸縮するため)~ また、元々setTextSize() の単位は SPとなっている。 public void setTextSize (float size) のため、引数はFLOATとなる。FLOATを表す'f'を後ろにつけて setTextSize (20.0f); とやる ~ ~ **参考 [#w163bc7a] http://www.javadrive.jp/android/radiobutton/index2.html~ http://www.hakkaku.net/articles/20091102-593 ~ ~ ''[[Back>Android GUI]]''