まずはレイアウトの修正を行います。
eclipseの「パッケージエクスプローラ」にて、「res」→「layout」を選択し、右クリック→「新規」→「Android XML ファイル」をクリックします。
「リソース・タイプ」で「Layout」
「ファイル」に「helloworld.xml」
「ルート要素」で「LinearLayout」
を選択し「完了」をクリックします。
グラフィカルレイアウトエディタが立ち上がりますが、基礎を学ぶため、今回はXMLファイルを直接編集する方法をとります。
画面下にある「helloworld.xml」をクリックし、XMLエディタを開きます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
これを以下のように変更します。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
次に、プログラムの修正を行います。eclipseの「パッケージエクスプローラ」にて、「src」→「パッケージ名」→「HelloWorldActivity.java」をダブルクリックします。
ソースエディタが立ち上がり、以下のように入力されていることを確認します。
package com.blogspot.logroid.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
extendsというのは、Java でのクラス継承を指します。
今回の場合、Activity クラスを継承しています。
onCreateメソッドはインスタンス化される際に呼び出されるメソッド(コンストラクタ)です。
このメソッドの中にsuperとありますが、これは継承元の Activity クラスのonCreateを呼び出しています。
その後、setContentViewメソッドで、レイアウトとビューの設定ファイルを指定しています。
先ほど作成したhelloworld.xmlを指定してみましょう。
package com.blogspot.logroid.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
}
}
以上で今回の作業は終わりです。
実行してみましょう。

0 件のコメント:
コメントを投稿