自分でオリジナルのアプリを作る際の基本!新しい画面の作り方を体験してみましょう。
このやり方を覚えると、好きなだけ画面を作ってアプリを拡張していくことができるようになります!
「Activity Name」を「TameshiActivity」に変更します。
「app > java > me.picnicapp.sample.picnicsample」に TameshiActivity が、
「app > manifest > AndroidManifest.xml」を開きます。
TameshiActivity に関する記述を、次のように書き換えます。
<activity android:name=".TameshiActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar">
</activity>
TameshiActivity のactivityノードに追加した属性については、以下の通りです。
「Layouts」内にある「LinearLayout(Horizontal)」を、「top_btn_map」の下にドラッグ&ドロップします。
このように、「top_btn_map」の下に「LinearLayout」が増えます。
次に「top_btn_map」を、新しく追加された「LinearLayout」に対してドラッグ&ドロップを行います。
このように、新しく追加された「LinearLayout」の配下に「top_btn_map」が入ります。Device Screen
FrameLayout
ImageView
LinearLayout
ImageView
LinearLayout
LinearLayout
top_btn_map
LinearLayout
top_btn_howto
top_btn_about
追加した LinearLayout の Properties で、「layout:height」を「wrap_content」に変更します。
次に、LinearLayout の「gravity > center」をONに変更します。
LinearLayout の配下に、もう一つボタンを追加します。
ボタンをレイアウトを整えていきます。
次に、先ほど追加したボタンを選択し、Properties の「layout:width」を「140dp」に、「layout:height」を「80dp」にそれぞれ変更します。
先ほど追加したボタンの「layout:margin > all」を「5dp」に変更します。
先ほど追加したボタンの「textSize」を「18sp」に変更します。
先ほど追加したボタンの「background」を選び、右側の参照ボタンをクリックします。
リソースの選択画面が出てくるので、画面左の「Drawable」から「button_top_normal_wide」を選び、OKボタンを押します。
先ほど追加したボタンの「textColor」を「#fff」に変更します。
先ほど追加したボタンの「text」を「ためし」に変更します。
先ほど追加したボタンの「id」を「top_btn_tameshi」に変更します。
AndroidのUIは、内部的にはXMLで記述されています。
「android:onClick="onClick"」を追加します。
完成形のXMLの中身は下記です。コピペするのではなく、自分の作ったものとdiffで確認してみましょう。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/top_bg" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:scaleType="fitCenter"
android:src="@drawable/top_image" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/top_btn_map"
android:layout_width="140dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:background="@drawable/button_top_normal_wide"
android:onClick="onClick"
android:text="@string/top_btn_map"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:layout_width="140dp"
android:layout_height="80dp"
android:text="ためし"
android:id="@+id/top_btn_tameshi"
android:layout_margin="5dp"
android:textSize="18sp"
android:background="@drawable/button_top_normal_wide"
android:onClick="onClick"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/top_btn_howto"
android:layout_width="140dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:background="@drawable/button_top_normal"
android:onClick="onClick"
android:text="@string/top_btn_howto"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:id="@+id/top_btn_about"
android:layout_width="140dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:background="@drawable/button_top_normal"
android:onClick="onClick"
android:text="@string/top_btn_about"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
画面内のボタンが押された時の条件判断に、以下の分岐を追加します。
else if (v.getId() == R.id.top_btn_tameshi) {
// ためし画面に遷移する
Intent intent = new Intent(getApplicationContext(), TameshiActivity.class);
startActivity(intent);
}
これで手順は全て終了です。