Eclipseでプロジェクトを新規作成したら、いろんなファイルができるけど、その中でとりあえず意識した方がいいファイルが3つあります。
src以下に生成される.javaファイル
package example.android.TextView;
import android.app.Activity;
import android.os.Bundle;
public class TextView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
res/layoutにできるmain.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
res/valuesにできるstrings.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TextView!</string>
<string name="app_name">TextView</string>
</resources>
とりあえず、上記グレーを塗った、青文字同士、赤文字同士は関係しているようです。
この実行結果がこれ。
じゃあとりあえず、TextViewを2つにしてみましょう。この場合、.javaファイルは変更不要で、main.xmlとstrings.xmlの2つを直します。
res/layoutにできるmain.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bye"
/>
</LinearLayout>
res/valuesにできるstrings.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TextView!</string>
<string name="bye">ばいばーい</string>
<string name="app_name">TextView</string>
</resources>
黒く塗ったところが新規追加コード。実行すると
となりました。TextViewって、こうやって使うんですね。