以下のような画面になっていればOKです。
次は、見た目は変わらないまま、スタイルを作ってみましょう。
スタイルとは、CSSみたいなもので、レイアウトXML内で使うViewの属性をまとめたものです。スタイルを使うことで、レイアウトの修正を簡単にすることができますし、ソースの可読性が上がります。
コードレビュー
では、コードを見てみましょう。Sample03プロジェクトの/res/layout/chapter13.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"
android:background="@drawable/back_img_repeat">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:adjustViewBounds="true"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="bottom"
android:layout_marginBottom="10dp">
<!--
ImageViewで定義している属性をstyleに書き換えてください。
ファイル名はstyles.xml
style名はTopBtnPortで作成してください。
-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="top|center_horizontal"
android:layout_marginBottom="10dp">
<ImageView
android:src="@drawable/regist"
android:layout_marginRight="10dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
<ImageView
android:src="@drawable/manage"
android:layout_marginRight="10dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
<ImageView
android:src="@drawable/photo_book"
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="top|center_horizontal">
<ImageView
android:src="@drawable/ranking"
android:layout_marginRight="10dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
<ImageView
android:src="@drawable/about"
android:layout_marginRight="10dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
<View
android:layout_width="90dp"
android:layout_height="90dp"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/adArea"
android:layout_width="fill_parent"
android:layout_height="50dp">
<ImageView
android:src="@drawable/nail_banner"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
Chapter12の答え合わせですが、ここではボタンでなくImageViewを使っています。ImageButtonというViewもあるのですが、ボタン用の縁などがつくので、そういうものがいらない場合は、ImageViewで作ってしまったほうが手っ取り早いと思っています。75行目のViewですが、ここでは表示する画像がないので、敢えて何も表示しないViewを配置しています。何も表示しないViewを配置するというのも、テクニックになってきます(最初思いつきませんでしたが、他の方のソースを見たりして、こういう方法もあるのかと気づきました)。
Chapter13の課題の「スタイルにまとめてみる」ですが、参考になるソースは、/res/layout/chapter14.xmlと、/res/values/sample_styles.xmlになります。参考にして挑戦してみてください。

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