Java (Android): テキストを画面に表示

device-2013-01-02-225322

package jp.feb19.snippets;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TextViewActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		// テキストビュー
		TextView tv = new TextView(this);
		tv.setText("ほげほげ");
		layout.addView(tv);
		
		
		// 右揃え赤背景テキストビュー
		LinearLayout layout2 = new LinearLayout(this);
		layout2.setGravity(Gravity.RIGHT);
		TextView tv2 = new TextView(this);
		tv2.setTextColor(Color.WHITE);
		tv2.setTextSize(20.0f);
		tv2.setBackgroundColor(Color.RED);
		tv2.setTypeface(Typeface.DEFAULT_BOLD);
		tv2.setPadding(10, 10, 10, 10);
		tv2.setGravity(Gravity.RIGHT);
		tv2.setText("ほげほげ");
		layout2.addView(tv2, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		layout.addView(layout2, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		
		
		setContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}
}