TextView を使ってみた

TextView は
java.lang.Object
  ↳ android.view.View
    ↳ android.widget.TextView
を使用します

というわけで、いろいろと値を指定して TextView をいじってみた

サンプルプログラム
  1. import android.app.Activity;  
  2. import android.graphics.Color;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5.   
  6. public class TextViewTest extends Activity {  
  7.   TextView textView1;  
  8.   TextView textView2;  
  9.   TextView textView3;  
  10.   TextView textView4;  
  11.   TextView textView5;  
  12.   @Override  
  13.   public void onCreate(Bundle savedInstanceState) {  
  14.     super.onCreate(savedInstanceState);  
  15.     setContentView(R.layout.textview);  
  16.   
  17.     textView1 = (TextView) findViewById(R.id.TextView01);  
  18.     textView2 = (TextView) findViewById(R.id.TextView02);  
  19.     textView3 = (TextView) findViewById(R.id.TextView03);  
  20.     textView4 = (TextView) findViewById(R.id.TextView04);  
  21.     textView5 = (TextView) findViewById(R.id.TextView05);  
  22.   
  23.     // TextView のテキストを設定  
  24.     textView1.setText("Test");  
  25.   
  26.     // TextView のテキストを取得  
  27.     String text = textView1.getText().toString();  
  28.   
  29.     // TextView のサイズを指定  
  30.     textView2.setTextSize(24);  
  31.   
  32.     // TextView のテキストの色を指定  
  33.     textView3.setTextColor(Color.RED);  
  34.   
  35.     // TextView のバックグラウンドの色を指定  
  36.     textView4.setBackgroundColor(Color.BLUE);  
  37.   
  38.     // TextView のバックグラウンドのリソースを指定  
  39.     textView5.setBackgroundResource(R.drawable.icon);  
  40.   }  
  41. }  

XMLリソース
  1. <textview   
  2.   android:id="@+id/TextView"   
  3.   android:layout_height="wrap_content"  
  4.   android:layout_width="wrap_content"  
  5.   android:text="TextView">  
  6. </textview>  

プログラムを実行すると...


こんな感じ!

0 件のコメント:

コメントを投稿