440 likes | 529 Views
PBL - ソフトウェア班 - 組み込みソフトウェアの開発 第二回. 前回行ったこと. Android アプリケーションの開発準備 簡単なアプリケーションの実行. 今回行うこと. 画面を設計. 前回の画面. 今回の目標画面. 画面のデザイン. Android では, 主に XML という言語を使ってデザインする. XML を説明する前に・・・ 実際にどういうものか見てみよう!!. XML の説明 (1 / 3). main.xml とは 画面のレイアウトを決めるファイル. ダブルクリック. main.xml をクリック.
E N D
PBL -ソフトウェア班-組み込みソフトウェアの開発第二回
前回行ったこと • Android アプリケーションの開発準備 • 簡単なアプリケーションの実行
今回行うこと • 画面を設計 前回の画面 今回の目標画面
画面のデザイン Android では, 主に XML という言語を使ってデザインする XML を説明する前に・・・ 実際にどういうものか見てみよう!!
XML の説明 (1 / 3) • main.xml とは • 画面のレイアウトを決めるファイル ダブルクリック main.xml をクリック
<LinearLayout> <TextView> XML の説明 (2 / 3) 構造を 表すと…
XML の説明 (3 / 3) <LinearLayout> <TextView> <LinearLayout> xmlns:android=… android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent " </LinearLayout> XMLで 表すと… <TextView android:layout_width="fill_parent" android:layout_height=“wrap_parent" android:text="@string/hello" /> • res/values/strings.xml の文字列を参照する
strings.xml の説明 (1 / 2) ダブルクリック strings.xml をクリック
strings.xml の説明 (2 / 2) • 3行目の「Hello World, Hello!」に注目 • @string/hello で参照される部分 @string/hello strings.xml 1: <?xml version="1.0" encoding="utf-8"?> 2: <resources> 3: <string name="hello">Hello World, Hello!</string> 4: <string name="app_name">sample</string> 5: </resources>
忘れた人のために (実行方法) 「Hello」 で右クリック エミュレータは自動的に 起動してくれる!! 「Android アプリケーション」 をクリックで実行
実際に変更してみよう • 問題 • 「Hello World. Hello!」を変更して,実際に出力される文字が変わるか確かめてみよう! • 変更後は左上の をクリックで保存 • もしくは Ctrl + s でも保存
ボタンの追加 (未来予想図) • About ボタンを追加
ボタンの追加 (構造の説明) <LinearLayout> <Button> <TextView> 構造を 表すと…
ボタンの追加 (main.xml を変更) 1: <?xml version="1.0" encoding="utf-8"?> 2: <LinearLayout xmlns:android="http://…" 3: android:orientation="vertical" 4: android:layout_width="fill_parent" 5: android:layout_height=“fill_parent"> 6: <TextView 7: android:layout_width="fill_parent" 8: android:layout_height="wrap_content" 9: android:text="@string/hello"/> 10: <Button 11: android:layout_width="fill_parent" 12: android:layout_height="wrap_content" 13: android:text="About"/> 14: </LinearLayout> ここを追加 するだけ
ボタンの追加 (動かしてみよう) • ボタンが表示されたかな? • ボタンを押したときの動作は次回
例 • 縦にボタンを配置 • 横にボタンを配置 • ボタンをいろいろ配置 • 表組みでいろいろ配置
Eclipse にインポート • 「既存プロジェクトをワークスペースへ」を選択→次へ をクリック • ファイル→インポートをクリック サンプルを使えるようにしよう 18
Eclipse にインポート • 参照をクリック • 解凍したフォルダのSampleを選択→ OK → 完了 19
インポートしたプロジェクトの説明 • // ここはコメントアウト • サンプル ここで実行したいサンプルを切り替えよう!! 20
例1 (縦にボタンを配置する) <LinearLayout> <Button> <Button> <Button> 構造を 表すと…
例1 (縦にボタンを配置する) <LinearLayout> <Button> <Button> <Button> <LinearLayout> xmlns:android=… android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent“ </LinearLayout> XMLで 表すと… <Button android:layout_width= android:layout_height= android:text="Button1“ /> <Button android:layout_width= android:layout_height= android:text="Button1“ /> <Button android:layout_width= android:layout_height= android:text="Button1“ />
例2 (横にボタンを配置する) <LinearLayout> <Button> <Button> <Button> 構造を 表すと…
例2 (横にボタンを配置する) <LinearLayout> <Button> <Button> <Button> <LinearLayout> xmlns:android=… android:orientation=“holizontal" android:layout_width="fill_parent" android:layout_height="fill_parent“ </LinearLayout> XMLで 表すと… <Button android:layout_width= android:layout_height= android:text="Button1“ /> <Button android:layout_width= android:layout_height= android:text="Button1“ /> <Button android:layout_width= android:layout_height= android:text="Button1“ />
例3 (ボタンをいろいろ配置する) <LinearLayout> android.orientation=“vertical" <LinearLayout> android.orientation=“vertical" <Button> <Button> <Button> <LinearLayout> android.orientation=“horizontal" <Button> <Button> <Button>
例4 (表組みでいろいろ配置してみる) <TableLayout> <TableRow> <Button> <TextView> <TableRow> <EditText> <TextView> <TableRow> <CheckBox> <TextView> <TableRow> <ImageView> <TextView>
遊んでみよう! • レイアウトや属性を色々変更してみよう • 3個作ろう!! • 紙を配るので,そこにまず作りたい画面の構造を書きましょう • 実際に作成しましょう
作成する際の注意点 • 新たにプロジェクトを1個作ること • プロジェクト名:PBL2 • アプリケーション名:subject2 • パッケージ名:pbl.android.学籍番号 • Activity:Subject • layout フォルダの中に複数 xml ファイルを作る • 新規 xml ファイルの作り方 • layoutフォルダを右クリック→新規→その他→android xml ファイル→好きなファイル名を入力
おわりに 作ったプロジェクトの持ち帰り方
エクスポート (1 / 3) • 作成したプロジェクトを自宅に持って帰れるようにする • 「ファイル・システム」を選択→次へ をクリック • Sample を右クリック • →エクスポートをクリック
エクスポート (2 / 3) • 参照をクリック • デスクトップを選択→OK
エクスポート (3 / 3) • 以上でプロジェクトのエクスポートは完了です • USB に入れて持って帰りましょう
課題 • 作成したXMLについて説明せよ.(どういう要素を利用したか,どういう属性を指定したか.) • PBL で作りたいアプリケーションを述べよ
課題 今後の PBL で作りたいアプリケーションを述べよ 1 で述べたアプリケーションに必要だと思われる画面を作成せよ 34
そのた:てーぶるれいあうと • TableLayout • 表形式で表示したい場合のレイアウト • 表の列数 • 一番セルの数が多いカラム数が採用される • TODOちぇっくぼっくすもここで説明
てーぶる 3列 2行
てーぶる 1: <?xml version="1.0" encoding="utf-8"?> 2: <TableLayout xmlns:android=http://... 3: android:layout_width="fill_parent“ 4: android:layout_height="fill_parent“ 5: android:stretchColumns=“1"> 6: <TableRow android:background="#696969"> 7: <TextView android:text="選択"/> 8: <TextView android:text="商品名"/> 9: <TextView android:text="数量"/> 9: </TableRow> 10: <TableRow> 11: <CheckBox/> 12: <TextView android:text="商品名"/> 13: <EditText/> 14: </TableRow> ・・・ 21: </TableLayout> 対応
その他 • RelativeLayout • 他のビューとの位置関係を指定することで表示される位置を決めていくレイアウト • EditText • 文字列を入力できる
その他 1: <?xml version="1.0" encoding="utf-8"?> 2: <RelativeLayout xmlns:android=http//… 3: android:layout_width="fill_parent" 4: android:layout_height="fill_parent"> 5: <TextView 6: android:id="@+id/label" 7: android:layout_width="fill_parent" 8: android:layout_height="wrap_content" 9: android:text="Type here:"/> 10: <EditText 11: android:id="@+id/entry" 12: android:layout_width="fill_parent" 13: android:layout_height="wrap_content" 14: android:layout_below="@id/label"/> 15: <Button 16: android:layout_width="wrap_content" 17: android:layout_height="wrap_content" 18: android:layout_below="@id/entry" 19: android:layout_alignParentRight="true" 20: android:text="OK"/> 21: </RelativeLayout> • android:id • ボタンやテキストビューにIDを設定する 対応 右寄せ
その他 入力切り替え
string.xml の変更 • 5行目を追加しよう • ボタンで表示する文字として使用 strings.xml 1: <?xml version="1.0" encoding="utf-8"?> 2: <resources> 3: <string name="hello">Hello World, Hello!</string> 4: <string name="app_name">sample</string> 5: <string name="about">About</string> 6: </resources>
XML の説明 (5 / 5) • @string • res/values/strings.xml の文字列を参照する fill_parent : main.xml 1: <?xml version="1.0" encoding="utf-8"?> 2: <LinearLayout xmlns:android="http://…“ 3: android:orientation="vertical“ 4: android:layout_width="fill_parent“ 5: android:layout_height=“fill_parent”> 6: <TextView 7: android:layout_width="fill_parent" 8: android:layout_height="wrap_content" 9: android:text="@string/hello"/> 10: </LinearLayout> 対応 wrap_content :