Android 開発用 Eclipse の環境で Android 用プロジェクトを作ると、
必要なファイル群がディレクトリと共に自動生成されます。
この生成において、リソースファイル用のフォルダは『res』の名前で作られます。
ここで作成したデフォルトのレイアウト名であれば、
「activity_Main.xml」のファイルに部品とそのレイアウト情報を記述します。
以下では、ファイルの変更過程の例を示します。
また、作品の名前を表現するリソース(strings.xml)の変更例を示します。
Eclipseが自動生成するこのレイアウト用xmlファイルは、
Viewと呼ばれる作品の画面構成を定義するものです。これはGUIの編集もできます。
その場合は、『Graphical Layout』タグを選んで Palette内の部品をドラックしてViewに配置する形態です。
以下で簡単な「おみくじ」の作品を作る例の最初のステップを示しめします。
このでは、部品の配置(レイアウト)やサイズを変更してそれがどのようにソースに反映するかを示します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="@string/hello_world" android:textSize="24sp" /> </RelativeLayout>デフォルトはRelativeLayoutと呼ばれるレイアウトに。部品を配置するようになっている。
dp | dip と書いても同じで、密度非依存ピクセル(density-independent pixels)の意味です。 160ドット/inchのスクリーンで、1dp=1px とする目安です。必ずしも正比例ではありません。 |
sp | Scale-independent pixelsで、これもスケール依存しない単位です。ユーザーの設定したフォントで1spの値が変わります。 |
px | ピクセルと呼ぶ。 スクリーン上のドット数で指定するサイズ |
mm | ミリの定規サイズ。 |
in | インチの定規サイズ。 |
pt | ポイントで、インチ定規のの1/72サイズ。 約 0.3527ミリ単位 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="@string/hello_world" android:textSize="24sp" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="50dp" android:ems="10" > <requestFocus /> </EditText> </RelativeLayout>ドラックによって基準となる部品が変化する。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="@string/hello_world" android:textSize="24sp" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="39dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="57dp" android:minWidth="200dp" android:text="ボタン" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="@string/hello_world"
android:textSize="24sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="39dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="57dp"
android:minWidth="200dp"
android:text="ボタン" />
<TextView
android:id=@+id/"@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="57dp"
android:text="ここに判定結果を表示します。" />
</RelativeLayout>
これは、『Graphical Layout』に切り替えると次のように反映しているのが確認できます。android:text="@string/hello_world"これは表示は、TextViewの表示文字列を指定している部分ですが、これには次に示すstrings.xmlの内容で決められます。