HOME技術AndroidAndroidアプリ:背景に画像を指定する

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”>

エミュレータを起動させると、以下のように反映されています。

関連記事

Androidアプリ:android:fullBackupContentの宣言

Android Studioで、以下のようなエラーメッセージが表示されました。 On SDK version 23 and up, your app data will be automaticall…続きを読む

Androidアプリ:タイトルバーを消す

Android Studioで、タイトルバーを消す方法です。 通常は、以下のように画面上部にタイトルバーが表示されています。 AndroidManifest.xmlを編集する android:them…続きを読む

Androidアプリを実機で確認する

AndroidアプリをAndroid Studioで開発した際に、実際のスマホの画面で動作確認する方法です。 スマホの設定 以下はArrows FX F-02Hの設定方法です(開発者向けオプションを表…続きを読む

Google広告でポリシー違反「句読点と記号」に対応する

モバイルアプリのGoogle広告のキャンペーンを設定しましたが、「ポリシー違反1件」と表示されました。 ポリシー違反「句読点と記号」 ポリシー違反の詳細を見ると、「句読点と記号」というヒントが表示され…続きを読む

Androidアプリ:アイコンの下に表示されるテキストの設定

Androidアプリをインストールした後に、スマホ画面に生成されるアイコンの下に表示されるテキストの設定方法です。 AndroidManifest.xml AndroidManifest.xmlを開き…続きを読む

Androidアプリ:数値型のViewの値がNullか0以下かを判定する

Android Studioで数値型のEditTextのViewを配置して、値を取得した場合に、その値がNull値(空)もしくは0以下であるか否かを判定する方法です。 Null判定にlength()を…続きを読む

Androidアプリ:クリックイベントとロングクリックイベントを取得する

Androidアプリで、クリックイベントとロングクリックイベントの2つを取得して処理する方法です。 OnClickListenerとOnLongClickListenerを併記 public clas…続きを読む

Androidアプリ:error: expected color reference but got (raw string)

Android Studioでビルドした際に、C:\android\project\***\app\src\main\res\values\styles.xml: AAPT: error: expec…続きを読む

Androidアプリ:レイアウトの途中にスクロールを入れる

Androidアプリで以下のようなレイアウトを組む方法です。 weightSumを使う 上、中央、下のパーツに分けて、上下のパーツは固定、中央のパーツはスクロール可能にします。 最初にLinearLa…続きを読む

Androidアプリ:画面遷移時に値を渡す

Androidで画面遷移時に値を渡す方法です。 数値を渡す場合 MainActivity.java public class MainActivity extends Activity { @Over…続きを読む

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny