Androidアプリ:背景に画像を指定する
Android Studioを使って、背景に画像を設定する方法です。
背景画像を格納する
今回は以下の画像を使います。
画像サイズなどは適当につくります。
Androidは様々な解像度がありますが、画像は自動的に画面にマッチするように表示してくれます。

画像の名前は、「background.png」とします。
この画像を「C:\Android\project\MyApplication\app\src\main\res\drawable」以下に保存します。
activity_main.xmlを編集する
レイアウト指定ファイルである、activity_main.xmlを開きます。
<android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”net.kuuur.myapplication.MainActivity”>
先頭に書かれている、この中に以下の一文を追加します。
android:background=”@drawable/background”
backgroundはファイル名になりますが、拡張子を指定する必要はありません。
まとめると以下のようになります。
<android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@drawable/background”
tools:context=”net.kuuur.myapplication.MainActivity”>
エミュレータを起動させると、以下のように反映されています。

関連記事