前回の1.1回目では
のようなレイアウトの画面を作成し、BMI(肥満度)を計算するような
ツールを作ろうというところまででした。
レイアウトの完成品はこちらです。
今回は、この画面レイアウトの作成について解説してきます。
この画面レイアウトを作成するにあたって、手を入れる必要のある
ソースは、『color.xml』と『main.xml』の2つになります。
『color.xml』は色の定義を行い、『main.xml』はこの画面のレイアウトを
定義します。
res\valuesを右クリックし、『新規(W)』⇒『その他(O)』から
『Android』の下の『Android XML File』を選択し、『次へ(N)』
『ファイル』に『color.xml』と入力し、『終了(F)』で『color.xml』が作成されます。
今回は、作成された『color.xml』を下記のように編集してみました。
color.xml---------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray">#808080</color>
<color name="lightgray">#D3D3D3</color>
</resources>
-----------------------------------------------------------------
grayという変数に#808080を、
lightgrayという変数に#D3D3D3を、定義しました。それだけです。
これらの変数は、『main.xml』で使用されます。
『main.xml』は下記のように編集しました。
main.xml----------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android
">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="身長"
android:gravity="left"
></TextView>
<EditText
android:id="@+id/EditText01"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="160"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15.0dp"
android:text="."
android:gravity="left"
android:layout_marginLeft="1dp"
></TextView>
<EditText
android:id="@+id/EditText02"
android:text="0"
android:layout_width="30.0dp"
android:layout_height="35.0dp"
android:textSize="15.0dp"
android:textStyle="normal"
android:typeface="normal"
android:hint="0"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="1dp"
></EditText>
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="体重"
android:gravity="left"
android:layout_marginLeft="10dp"
></TextView>
<EditText
android:id="@+id/EditText03"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="50"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15.0dp"
android:text="."
android:gravity="left"
android:layout_marginLeft="1dp"
></TextView>
<EditText
android:id="@+id/EditText04"
android:text="0"
android:layout_width="30.0dp"
android:layout_height="35.0dp"
android:textSize="15.0dp"
android:textStyle="normal"
android:typeface="normal"
android:hint="0"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="1dp"
></EditText>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<Button
android:text="BMI(肥満度)計算"
android:id="@+id/Button01"
android:layout_width="200.0dp"
android:layout_height="60.0dp"
android:textSize="20dp"
android:textStyle="bold"
android:typeface="normal"
android:layout_marginTop="20dp">
</Button>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="monospace"
android:textSize="20.0dp"
android:text="■あなたのBMI(肥満度)"
android:gravity="left"
android:layout_marginTop="30dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout04"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16.0dp"
android:textColor="@color/gray"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout05"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="monospace"
android:textSize="16.0dp"
android:text="■BMI(肥満度)から見たあなたの標準体重"
android:gravity="left"
android:layout_marginTop="25dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout06"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView61"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"
android:textSize="16.0dp"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout07"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView71"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16.0dp"
android:textStyle="bold"
android:typeface="monospace"
android:text="■現在の体重-標準体重"
android:gravity="left"
android:layout_marginTop="15dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout08"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"
android:textSize="16.0dp"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
-----------------------------------------------------------------
なにやら長々と書いてあって何のことかさっぱりな感じですが、
内容はたいしたことありません。
上記を図であらわしたものが、Eclipseの『アウトライン』に表示
されています。
緑色のT,E,Bの記号のついているものが、画面に表示される
ものです。
T(TextView)が文字の表示、
E(EditView)が文字の入力
B(Button)が何か動作をさせるボタンです。
アウトラインや画面に表示されているTextViewやEditViewやButton、
それらを囲っているLinearLayoutやTableLayoutにプロパティーを設定
することで、入力制限や文字のフォントの変更などができます。
たとえば、今回使用したLinearLayoutは、このように定義していますが、
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android
">
『android:orientation="vertical"』は、LinearLayout内のViewなどの
コンポーネントを垂直に(縦に)並べるような指定になります。
初期値(デフォルト)は水平(横方向)への配置になります。
今回の例で言えば、TableLayout毎には縦方向、TableLayout内では
横方向の指定になっています。
次にTableLayoutを見てみましょう。
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/TableLayout01"は、このコンポーネントのID
を指定します。このTableLayoutのIDは、『TableLayout01』に
なります。
android:layout_width="wrap_content"はこのコンポーネントの
横幅のサイズについてGUI要素を表示するための最小のサイズで
画面に表示します。
android:layout_height="wrap_content">は縦幅のサイズについて
になります。
中の要素に応じてサイズを変更してくれるので、便利かもしれません。
次にTextViewを見てみましょう。
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="身長"
android:gravity="left"
></TextView>
android:textSize="20.0dp"
テキストのフォントサイズを指定します。dpは単位です。
android:gravity="left"はコンポーネントを左寄せにします。
あと2つです、EditTextを見てみましょう。
これは、テキストの入力に使います。
<EditText
android:id="@+id/EditText01"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="160"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>
android:layout_width="60.0dp"はこのコンポーネントの横幅の
サイズを明示的に指定します。
android:layout_height="40.0dp"は縦幅です。
android:textSize="20.0dp"は中のテキストのサイズを指定します。
android:textStyle="normal"はテキストのスタイルを、
android:typeface="normal"はテキストの書体を指定します。
何も入力されていない場合に、どのように入力するかヒントを
表示することができます。
android:textColorHint="@color/lightgray"はヒントの色を
color.xmlから指定し、
android:hint="160"はヒントに表示する文字を指定します。
今回でいえば、明るい灰色で160と表示します。
android:digits="0123456789"は、0123456789"の入力以外は
受け付けず、
android:inputType="number"は数字しか、入力の対象にならない
ようにします。
android:maxLength="3"は、最大桁数の指定です。身長や体重は
3桁まででよさそうです。
android:maxLines="1"は最大行数を指定します。
android:layout_marginLeft="5dp"は、右のコンポーネントとの間隔です。
最後にButtonです。
<Button
android:text="BMI(肥満度)計算"
android:id="@+id/Button01"
android:layout_width="200.0dp"
android:layout_height="60.0dp"
android:textSize="20dp"
android:textStyle="bold"
android:typeface="normal"
android:layout_marginTop="20dp">
</Button>
android:layout_marginTopは、上のコンポーネントとの間隔です。
他はいままででてきたプロパティーと同じです。
これで、main.xmlの内容は、ほぼ理解できるようになりました。
詳しくは下記のアドレスから調べられては如何でしょうか。
今更ですが、android:textに書いている文字列なんかは、
Strings.xmlに定義すればよかったですね。
次回までに修正しておきます。
次回は、この画面で計算できるようにしていきましょう。
ではでは。
スリムなフォルムがスタイリッシュでオシャレなビジネスシューズ!レビューのお約束で防水スプ... 価格:14,490円(税込、送料込) |